Project

General

Profile

Feature #6365

Updated by nobu (Nobuyoshi Nakada) over 12 years ago

=begin 
 I find myself constantly wanting to check whether one string is included within an array of strings. 
 It is certainly possible (and also fast) to do that in Ruby with something like this: (({["foo", "bar"].include?("foo")})) ["foo", "bar"].include?("foo") 
 But I don't think it reads very nice :( 
 Because what I actually want to test is, whether my string is included in the array and NOT the other way around. 

 What do you think about something like the following two solutions? 

   class 

 (({class String 
     
   # create a new method 
     
   def included_in?(array) 
       
     array.include?(self) 
     
   end 

     

   # -- OR -- 

     

   # change the current String#include? method 
     
   def include?(parameter) 
       
     if parameter.is_a? Array 
         
       parameter.include?(self) 
     else 
       else 
         super 
       end 
     end 
   end 
 end})) 

 I know it's just a minor code vanity issue, but since it's one of Ruby's main features, I wanted to bring it up. 

 The (pseudo) code can also be found here:    ((<"https://gist.github.com/1181246"|URL:https://gist.github.com/1181246>)) https://gist.github.com/1181246 
 =end 

Back