Let's say you have conditions with multiple ORs, for example.
if action == 'new' || action == 'edit' || action == 'update'
Another way to write this:
if ['new', 'edit', 'action'].include?(action)
but it seems like a “reverse” way of writing logic.
Is there a built-in way to do something like:
if action.equals_any_of?('new', 'edit', 'action')
?
Update - I am very addicted to this small snippet:
class Object
def is_included_in?(a)
a.include?(self)
end
end
Update 2 - improvements based on the comments below:
class Object
def in?(*obj)
obj.flatten.include?(self)
end
end
source
share