开发者

is this a valid ruby syntax?

开发者 https://www.devze.com 2022-12-09 11:39 出处:网络
if step.include? \"apples\" or \"banana\" or \"cheese\" say \"yay\" end开发者_开发问答 Several issues with your code.
if step.include? "apples" or "banana" or "cheese"
say "yay"
end开发者_开发问答


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


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/)


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}


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.


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.


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


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.)


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
0

精彩评论

暂无评论...
验证码 换一张
取 消