i have a string variable which can only contain 6 different values. I want to check if it contains one of the first 4 values or one of the 2 second values.
Is there a more elegant way than this:
if string.eql? 'val1' || string.eql? 'val2' || string.eql? 'val3' || string.eql? 'val4'
...
elsif string.eql? 'val5' || string.eql? 'val6'
...
end
Maybe something like if string is in ['val1', 'val2', 'val3', 'val4']
?
You could use include?
:
if ['val1', 'val2', 'val3', 'val4'].include?(string)
case string
when *%w[val1 val2 val3 val4]
...
else
...
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With