On 2009/05/25 13:41, Roger Pack wrote:

> Add an Object#in? method to complement Enumerable#include?
>
> Background: currently if you want to test for membership you have to
> do it "backward" by using include? on the array.
>
> ex:
>
> if STUFF.include?(a)
>   puts 'yes'
> else
>   puts 'no'
> end
>
> Clearer to the reader (at least to my eyes) would be

I don't think you can say which one is clearer in general, but I very 
much agree that having both is a plus, especially also because

   if a.in?(STUFF)

may simplify to

   if in? STUFF

in some contexts.

Regards,    Martin.

> if a.in?(STUFF)
>   puts 'yes'
> else
>   puts 'no'
> end
>
> proposed definition (from [3])
>
> module Kernel
>    # Is self included in other?
>    #
>    #   5.in?(0..10)       #=>  true
>    #   5.in?([0,1,2,3])   #=>  false
>    #
>    def in?(other)
>      other.include?(self)
>    end
>
> end
>
> currently several developers said they use this idiom already [1,2,3],
> facets has it, and I have found it quite useful in the past, therefore
> propose its incorporation into core.
>
> drawbacks: existing 'in?' methods would become ambiguous--I'd imagine
> this is rare so hopefully a limited impact.
>
> Thoughts?
> Thanks.
> -=r
>
> [1] http://www.ruby-forum.com/topic/184011
> [2] http://snippets.dzone.com/posts/show/3516
> [3] http://facets.rubyforge.org/doc/api/core/classes/Kernel.html#M000425
>
>

-- 
#-# Martin J. D?rst, Professor, Aoyama Gakuin University
#-# http://www.sw.it.aoyama.ac.jp   mailto:duerst / it.aoyama.ac.jp