Re: Array#uniq vs Array#-
Clavius Tales <[email protected]>
| Newsgroups | gmane.comp.lang.ruby.general |
|---|---|
| Message-ID | <CAAsQKqWTfshVtZYrWkwrx9GvCtgsRkS9819tCxzfQJtaeHCnYg@mail.gmail.com> |
Hi, Gerald. The RDoc also says that Array#- uses both #hash and #eql? methods. But in my example I implemented just the #eql? method and worked: [Any.new] - [Any.new] => [] In other words: why Array#- do not require that elements implement #hash method for make equality comparison, as said in documentation? Cheers. On Tue, Apr 16, 2019 at 8:04 PM Gerald Bauer <[email protected]> wrote: > > Hello, > > Just looked at the 1st result in the "ruby uniq" Google search, > that is, the official Array RDoc [1]. > > If you look at Array#uniq it tells you: > > > It compares values using their hash and eql? methods for efficiency. > > If you try it. Add to your Any class: > > def hash() 1; end > > And retry: > > [Any.new,Any.new].uniq > => [#<Any:0x111>] > > Now it works as you expect. Cheers. Prost. > > [1] https://ruby-doc.org/core-2.5.0/Array.html#method-i-uniq > > Unsubscribe: <mailto:[email protected]?subject=unsubscribe> > <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk> Unsubscribe: <mailto:[email protected]?subject=unsubscribe> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>