ruby - How do I say "if x == A or B or C" as succinctly as possible? -


i'm pretty sure ruby has idiom that.

i have many places in code say

if (x == a) || (x == b) || (x ==c)   do_something else   do_something_else end 

i know

case x when a, b, c    do_something else    do_something_else end 

but prefer using if else if there's nice idiom make more succinct.

you can tidy case statement bit more this

case x when a, b, c do_something else do_something_else end 

or if repeating pattern, roll method on object

class object   def is_one_of?(*inputs)     inputs.include?(self)   end end 

then use as

if x.is_one_of?(a, b, c)   do_something else   do_something_else end 

Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -