if step.include? "apples" or "banana" or "cheese"
say "yay"
end
if step.include? apples or banana or cheese say yay end
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Several issues with your code.
This expression evaluates to:
Because Ruby treats all values other than
falseandnilas true, this expression will always be true. (In this case, the value"banana"will short-circuit the expression and cause it to evaluate as true, even if the value of step does not contain any of these three.)Your intent was:
However, this is inefficient. Also it uses
orinstead of||, which has a different operator precedence, and usually shouldn’t be used inifconditionals.Normal
orusage:A better way of writing this would have been:
This uses a regular expression, which you’re going to use a lot in Ruby.
And finally, there is no
saymethod in Ruby unless you define one. Normally you would print something by callingputs.So the final code looks like: