Is this valid ruby syntax?
Several problems with your code.
step.include? "apples" or "banana" or "cheese"
This expression evaluates to:
step.include?("apples") or ("banana") or ("cheese")
Since Ruby treats all values other than false
and nil
as true, this expression will always be true. (In this case, the value "banana"
will be a short-circuit expression and cause it to evaluate to true, even if the step value contains none of these three.)
Your intention:
step.include? "apples" or step.include? "banana" or step.include? "cheese"
However, this is ineffective. Also it uses or
instead ||
, which has a different operator precedence and shouldn't normally be used in conditional expressions if
.
Common or
usage:
do_something or raise "Something went wrong."
The best way to write this would be this:
step =~ /apples|banana|cheese/
It uses the regular expression you are going to use in Ruby.
Finally, there is no method in Ruby say
unless you define one. Usually you print something by calling puts
.
So the final code looks like this:
if step =~ /apples|banana|cheese/
puts "yay"
end
source to share
This is definitely not what you think you want. The method include?
takes on a value String
that is not what it creates "apples" or "banana" or "cheese"
. Try this instead:
puts "yay" if ["apples", "banana", "cheese"].include?(step)
But it is not clear from the context which step should be. If it's just one word, then it's okay. If it could be a whole sentence, try joel.neely's answer.
source to share