Test whether a variable equals either one of two values

Your first method is idiomatic Ruby. Unfortunately Ruby doesn’t have an equivalent of Python’s a in [1,2], which I think would be nicer. Your [1,2].include? a is the nearest alternative, and I think it’s a little backwards from the most natural way.

Of course, if you use this a lot, you could do this:

class Object
  def member_of? container
    container.include? self
  end
end

and then you can do a.member_of? [1, 2].

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)