Re: Array#uniq vs Array#-
Cristian Rivera <[email protected]>
| Newsgroups | gmane.comp.lang.ruby.general |
|---|---|
| Message-ID | <[email protected]> |
Hi, I believe it removes all uniq objects of the same object type but I could be wrong. This is of course inferred by reading the .c source which I am not an expert at! Sent from my iPhone > On Apr 16, 2019, at 7:46 PM, Clavius Tales <[email protected]> wrote: > > Hi, Cristian. > > > In the last part of your example the `-` minus in Ruby removes all unique objects from the Arrays before actually performing the subtraction therefore giving you an empty Array. It removes the unique values between the two because you obviously can’t subtract two things that aren’t equal. > > From what I understand of what you said, it should be this: > > [:a] - [:b] > => [] > > [:a] - [1] > => [] > > But it is: > > [:a] - [:b] > => [:a] > > [:a] - [1] > => [:a] > > I've enhanced my example: > https://repl.it/repls/FantasticIrritatingObjectcode > > Cheers. > > > >> On Tue, Apr 16, 2019 at 8:20 PM Cristian Rivera <[email protected]> wrote: >> It comes down to a matter of uniqueness. In the first part of your example `uniq` returns an Array with both elements because it uses the object hash AND `eql?` to determine uniqueness. In the last part of your example the `-` minus in Ruby removes all unique objects from the Arrays before actually performing the subtraction therefore giving you an empty Array. It removes the unique values between the two because you obviously can’t subtract two things that aren’t equal. Even though you are setting `eql?` in your class the object hash between the two still varies. >> >> Thanks, >> Cristian Rivera >>> On Apr 16, 2019, 5:30 PM -0400, Clavius Tales <[email protected]>, wrote: >>> Hi. >>> >>> https://repl.it/repls/FantasticIrritatingObjectcode >>> >>> Do you know why? >>> >>> Thanks. >>> >>> 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> > > 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>