tags:

views:

65

answers:

3

Is there a method that let me compare one String with multiple others in Ruby? I really would like to do something like this:

myString.eql?(["string1","string2","string3"])
+5  A: 
["string1","string2","string3"].include? myString
mipadi
+2  A: 

You could use Array#include? to see if the array includes the string:

%w(string1 string2 string3).include?(myString)
Daniel Vandersluis
+3  A: 

I find myself wanting this a lot, so I added a String method to be able to do it more idiomatically:

class String
  def among?(*array)
    array.flatten.include?(self)
  end
end

Then

myString.among?("string1","string2","string3")
glenn mcdonald