Ruby:我可以在一个语句中多次使用“or”(||)吗?

时间:2021-08-16 16:47:05

Hi I'm trying to make a blackjack game using Ruby and am trying to make the values of the picture cards all = 10. Is it okay to use the code I use below to accomplish this? (this is all happening in my Card class)

你好,我正在尝试用Ruby做一个21点游戏,我正在尝试使图片卡的值都= 10。是否可以使用下面的代码来实现这一点?(这一切都发生在我的卡片上)

def value
  if @number == ("jack" || "queen" || "king")
    10
  else
    @number.to_i
  end
end

3 个解决方案

#1


9  

You can, but not the way you are using it. You either need to use the entire boolean expression in each portion:

你可以,但不是你使用它的方式。您需要在每个部分中使用整个布尔表达式:

if @number == "jack" || @number == "queen" || @number == "king"

or you can make it simpler by checking the contents of an array:

或者可以通过检查数组的内容来简化:

if ["jack", "queen", "king"].include?(@number)

#2


5  

The parens group things that should be evaluated before other things. So, the above says, evaluate:

parens将应该在其他事情之前进行评估的事情分组。因此,上面说,评估:

 ("jack" || "queen" || "king")

and return the results. Lets try that in irb:

并返回结果。让我们在irb上试试:

irb(main):004:0> ("jack" || "queen" || "king")
=> "jack"

Since "jack" is truthy there's no need to look at the rest of the list and "jack" is returned.

因为“jack”是真实的,所以没有必要看列表的其余部分,“jack”被返回。

This will work fine as long as @number is equal to "jack" but not so much for the other values. You want to compare @number against each value until you get a match or exhaust the list. See @PinneyM's answer of

只要@number等于“jack”,但其他值不等于@number,这就没问题。您希望将@number与每个值进行比较,直到得到匹配或结束列表。看到@PinneyM回答的

(@number == "jack") || (@number == "queen") ...

#3


2  

That is a valid ruby snippet, but it doesn't do what you think it does: it first evaluates

这是一个有效的ruby代码片段,但是它不像您认为的那样做:它首先评估。

 ("jack" || "queen" || "king")

which evaluates to "jack" as that is the first non false/nil value. It then compares @card to this, so your code is equivalent to

它的结果是“jack”,因为这是第一个非false/nil值。然后它将@card与这个进行比较,因此您的代码是等价的

def value
  if @number == 'jack'
    10
  else
    @number.to_i
  end
end

You could compare each in turn (@number == 'jack') || (@number == 'queen') ||..., you could use %w(jack queen king).include?(@number) or you could use a case statement:

您可以依次比较每一个(@number = 'jack') || (@number = 'queen') ||……,您可以使用%w(jack queen king).include?(@number),也可以使用case语句:

def value
  case @number
  when 'jack', 'queen', 'king'
    10
  else
    @number.to_i
  end
end

#1


9  

You can, but not the way you are using it. You either need to use the entire boolean expression in each portion:

你可以,但不是你使用它的方式。您需要在每个部分中使用整个布尔表达式:

if @number == "jack" || @number == "queen" || @number == "king"

or you can make it simpler by checking the contents of an array:

或者可以通过检查数组的内容来简化:

if ["jack", "queen", "king"].include?(@number)

#2


5  

The parens group things that should be evaluated before other things. So, the above says, evaluate:

parens将应该在其他事情之前进行评估的事情分组。因此,上面说,评估:

 ("jack" || "queen" || "king")

and return the results. Lets try that in irb:

并返回结果。让我们在irb上试试:

irb(main):004:0> ("jack" || "queen" || "king")
=> "jack"

Since "jack" is truthy there's no need to look at the rest of the list and "jack" is returned.

因为“jack”是真实的,所以没有必要看列表的其余部分,“jack”被返回。

This will work fine as long as @number is equal to "jack" but not so much for the other values. You want to compare @number against each value until you get a match or exhaust the list. See @PinneyM's answer of

只要@number等于“jack”,但其他值不等于@number,这就没问题。您希望将@number与每个值进行比较,直到得到匹配或结束列表。看到@PinneyM回答的

(@number == "jack") || (@number == "queen") ...

#3


2  

That is a valid ruby snippet, but it doesn't do what you think it does: it first evaluates

这是一个有效的ruby代码片段,但是它不像您认为的那样做:它首先评估。

 ("jack" || "queen" || "king")

which evaluates to "jack" as that is the first non false/nil value. It then compares @card to this, so your code is equivalent to

它的结果是“jack”,因为这是第一个非false/nil值。然后它将@card与这个进行比较,因此您的代码是等价的

def value
  if @number == 'jack'
    10
  else
    @number.to_i
  end
end

You could compare each in turn (@number == 'jack') || (@number == 'queen') ||..., you could use %w(jack queen king).include?(@number) or you could use a case statement:

您可以依次比较每一个(@number = 'jack') || (@number = 'queen') ||……,您可以使用%w(jack queen king).include?(@number),也可以使用case语句:

def value
  case @number
  when 'jack', 'queen', 'king'
    10
  else
    @number.to_i
  end
end