tags:

views:

123

answers:

8
if step.include? "apples" or "banana" or "cheese"
say "yay"
end
+3  A: 

The last two terms appear to Ruby as true, rather than having anything to do with the include? phrase.

Assuming that step is a string...

step = "some long string with cheese in the middle"

you could write something like this.

puts "yay" if step.match(/apples|banana|cheese/)
joel.neely
If it's in a conditional (like yours) you could also do: `puts "yay" if step =~ /apples|banana|cheese/`
thenduks
(for those that don't know, because `puts "yay" if 0` will execute the puts, but `puts "yay" if nil` wont)
thenduks
+1  A: 

It's definitely not what you appear to be wanting. The include? method takes in a String, which is not what "apples" or "banana" or "cheese" produces. Try this instead:

puts "yay" if ["apples", "banana", "cheese"].include?(step)

But it's unclear from the context what step is supposed to be. If it's just the single word, then this is fine. If it can be a whole sentence, try joel.neely's answer.

Chris Bunch
+2  A: 

Here's a way to call step.include? on each of the arguments until one of them returns true:

if ["apples", "banana", "cheese"].any? {|x| step.include? x}
jleedev
A: 

I'll add some parentheses for you:

if (step.include? "apples") or ("banana") or ("cheese")
    say "yay"
end

(That would be why it's always saying "yay" -- because the expression will always be true.)

Anthony Mills
+6  A: 

Several issues with your code.

step.include? "apples" or "banana" or "cheese"

This expression evaluates to:

step.include?("apples") or ("banana") or ("cheese")

Because Ruby treats all values other than false and nil as 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:

step.include? "apples" or step.include? "banana" or step.include? "cheese"

However, this is inefficient. Also it uses or instead of ||, which has a different operator precedence, and usually shouldn't be used in if conditionals.

Normal or usage:

do_something or raise "Something went wrong."

A better way of writing this would have been:

step =~ /apples|banana|cheese/

This uses a regular expression, which you're going to use a lot in Ruby.

And finally, there is no say method in Ruby unless you define one. Normally you would print something by calling puts.

So the final code looks like:

if step =~ /apples|banana|cheese/
  puts "yay"
end
Bob Aman
do i add /i for case insensitive ? is this like regex ? ty.
gpwu
Yes, it would be `step =~ /apples|banana|cheese/i`
Adam Lassek
+1  A: 

The closest thing to that syntax that would do what you appear to want would be something like:

  if ["apples", "banana", "cheese"].include?(step)
    puts "yay"
  end

But one of the other suggestions using a regex would be more concise and readable.

Justin Blake
A: 

Just to add another side to this...

If step is an Array (as calling include? seems to suggest) then maybe the code should be:

if (step - %w{apples banana cheese}) != step
  puts 'yay'
end
thenduks
The most confusing and computation intensive variant.
Maxim Kulkin
I don't know about confusing. That's pretty silly. As far as 'computation intensive'... well, I wasn't exactly putting effort into an efficient algorithm, but it isn't that bad.
thenduks
+1  A: 

Assuming step is an Array or a Set or something else that supports set intersection with the & operator, I think the following code is the most idiomatic:

unless (step & ["apples","banana","cheese"]).empty?
  puts 'yay'
end
Ken Bloom