From: Alex Gutteridge Date: 2007-10-10T15:07:03+09:00 Subject: Re: will #in? be in ruby2? (was Re: Finding shared elements between to arrays.) On 10 Oct 2007, at 14:06, Peņa, Botp wrote: > From: Devi Web Development [mailto:devi.webmaster@gmail.com] > # > compare: > # > a3 = a1.select { |e| e.in? a2} > # > > # I really don't like that proposal. It's not a property of e > # that it is in a2, its a property of a2 that it contains e. > > pls enlighten my poor brain :( > > i just want to ask if e is in a2. same way like i want to ask eg if > e==2 ... in fact, i find #in? so generic... > > or maybe you want a keyword only like msbasic's > > e in a2 > > ? > > kind regards -botp If I understand you correctly the :in? method would have to be defined in Object (so that all Objects could be tested). So something like: irb(main):001:0> class Object irb(main):002:1> def in?(enum) irb(main):003:2> enum.include?(self) irb(main):004:2> end irb(main):005:1> end => nil irb(main):006:0> 1.in? [1,2,3] => true irb(main):007:0> 4.in? [1,2,3] => false Daniel's point (I think) is that this pollutes the top level Object with a property which for many (most?) Objects is never relevant. If you never put it an Object into a collection then the idea of being 'in' something makes no sense. On the other hand it is always an intrinsic part of an Enumerable implementing class (by definition a collection of Objects) that you may often need to know whether it 'includes' a specified object. I'm not sure why the includes construct rocks your brain though. Both read very logically to me (as a native English speaker admittedly): a3 = a1.select { |e| a2.include?(e) } reads as: 'select' from a1 those e where a2 'includes' e. a3 = a1.select { |e| e.in? a2} reads as: 'select' from a1 those e where e is 'in' a2. That said I always write this as: a3 = a1 & a2 Alex Gutteridge Bioinformatics Center Kyoto University