In? Method - Reverse Perspective Of Include?
Join the DZone community and get the full member experience.
Join For FreeYes, this is lame, but I constantly find myself reverting to Python land where I used to test like this:
if this in that...
In Ruby, I have to reverse the perspective (which is hard for my wee little brain) and do this instead:
if that.include? this...
It occurred to me I can make Ruby do the thinking for me by extending the Object class. Thoughts?
class Object
def in?(array)
array.include? self
end
end
Example use:
if 1.in? [2, 9, 1, 4]
puts "Don't be so lazy, Tim."
end
Opinions expressed by DZone contributors are their own.
Comments