Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #7040
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Newsgroups | comp.lang.ruby |
| Subject | Re: Set.new([1,2,3]) == Set.new([1,2.0,3]) returns false |
| Date | 2014-10-16 08:34 +0200 |
| Message-ID | <ca9780Flj6pU1@mid.individual.net> (permalink) |
| References | <27215c23-bae9-47d1-bc00-db93fc96e6f2@googlegroups.com> |
On 16.10.2014 01:54, Aarti Parikh wrote: > I get that Set is built with a Hash and that's why this happens, but should it work like this? Do other languages handle it differently? > > irb(main):019:0> Set.new([1,2,3]) == Set.new([1,2.0,3]) > => false > irb(main):020:0> Set.new([1,2,3]) == Set.new([1,2,3]) > => true > irb(main):021:0> 2 == 2.0 > => true > irb(main):022:0> [2] == [2.0] > => true irb(main):001:0> 2 == 2.0 => true irb(main):002:0> 2.eql? 2.0 => false In other words: == is a method which conveniently matches integers and floats of a similar numeric value but the equivalence relation used by Hash takes the type into account. It may be unusual but I think it is very useful the way it is. Btw. you can also do [1,2,3].to_set. :-) Kind regards robert
Back to comp.lang.ruby | Previous | Next — Previous in thread | Find similar
Set.new([1,2,3]) == Set.new([1,2.0,3]) returns false Aarti Parikh <aarti@kitereaders.com> - 2014-10-15 16:54 -0700 Re: Set.new([1,2,3]) == Set.new([1,2.0,3]) returns false Robert Klemme <shortcutter@googlemail.com> - 2014-10-16 08:34 +0200
csiph-web