Ruby - alternative style for determining if object is contained within a list?

ruby

Solution

Use a regex?

action =~ /new|edit|action/

Or:

action.match /new|edit|action/

Or just write a simple utility method that's semantically-meaningful in the context of your app.

Problem

Let's say you have a conditions with multiple ORs, e.g. ``` if action == 'new' || action == 'edit' || action == 'update' ``` Another way to write this is: ``` if ['new', 'edit', 'action'].include?(action) ``` but that feels like a 'backwards' way to write the logic. Is there any built-in way to do something like: ``` if action.equals_any_of?('new', 'edit', 'action') ``` ? Update - I'm quite keen on this little 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 ```

Original source