Ruby 三值运算符输出True选项(当为False时)

Ruby 三值运算符输出True选项(当为False时),ruby,ternary-operator,string-interpolation,Ruby,Ternary Operator,String Interpolation,下面的程序输出歌曲“99瓶啤酒”的歌词 当歌曲达到只剩下一瓶时,它使用单数形式的“瓶子”。为了适应这种情况,我使用了三元运算符在任何给定时刻选择正确的情况 然而,当程序中的啤酒瓶数达到1时,最后一句话仍然输出“瓶子”,即使三元运算符的计算结果明显为false 我在IRB中用啤酒瓶=1测试了三元运算符,它正确地输出了假选项:“瓶子” 非常感谢您帮助理解为什么会发生这种情况 beer_bottles = 99 while beer_bottles >= 2 do plural = "bo

下面的程序输出歌曲“99瓶啤酒”的歌词

当歌曲达到只剩下一瓶时,它使用单数形式的“瓶子”。为了适应这种情况,我使用了三元运算符在任何给定时刻选择正确的情况

然而,当程序中的
啤酒瓶数达到1时,最后一句话仍然输出“瓶子”,即使三元运算符的计算结果明显为false

我在IRB中用
啤酒瓶=1
测试了三元运算符,它正确地输出了假选项:“瓶子”

非常感谢您帮助理解为什么会发生这种情况

beer_bottles = 99

while beer_bottles >= 2 do
  plural = "bottles"

  singular = "bottle"

  plural_or_singular = beer_bottles > 1 ? plural : singular

  puts "#{beer_bottles} #{plural_or_singular} of beer on the wall, #{beer_bottles} #{plural_or_singular} of beer."

  beer_bottles -= 1

  puts "BOTTLE COUNT: #{beer_bottles}"

  puts "Take one down and pass it around, #{beer_bottles} #{plural_or_singular} of beer on the wall."
end

啤酒瓶-=1
更新后,您不会再次计算
复数或单数

解决方案:

beer_bottles = 99

while beer_bottles >= 2 do
  plural = "bottles"

  singular = "bottle"

  plural_or_singular = beer_bottles > 1 ? plural : singular

  puts "#{beer_bottles} #{plural_or_singular} of beer on the wall, #{beer_bottles} #{plural_or_singular} of beer."

  beer_bottles -= 1
  plural_or_singular = beer_bottles > 1 ? plural : singular
  puts "BOTTLE COUNT: #{beer_bottles}"

  puts "Take one down and pass it around, #{beer_bottles} #{plural_or_singular} of beer on the wall."
end

啤酒瓶-=1
更新后,您不会再次计算
复数或单数

解决方案:

beer_bottles = 99

while beer_bottles >= 2 do
  plural = "bottles"

  singular = "bottle"

  plural_or_singular = beer_bottles > 1 ? plural : singular

  puts "#{beer_bottles} #{plural_or_singular} of beer on the wall, #{beer_bottles} #{plural_or_singular} of beer."

  beer_bottles -= 1
  plural_or_singular = beer_bottles > 1 ? plural : singular
  puts "BOTTLE COUNT: #{beer_bottles}"

  puts "Take one down and pass it around, #{beer_bottles} #{plural_or_singular} of beer on the wall."
end

最安全的做法是在输出变量时进行检查。您只需在打印最后一行之前向下移动三元

我很想把它提取到一个单独的方法中。事实上,这就是Rails对
pluralize
的作用。我们可以创建自己的简化版本:

def pluralize(count, noun)
  "#{count} #{count==1 ? noun : noun + 's'}"
end
那么您的代码可能如下所示:

99.downto(1) do |n|
  puts "#{pluralize(n, "bottle")} of beer on the wall, #{pluralize(n, "bottle")} of beer."
  puts "Take one down and pass it around, #{pluralize(n-1, "bottle")} of beer on the wall."
end

最安全的做法是在输出变量时进行检查。您只需在打印最后一行之前向下移动三元

我很想把它提取到一个单独的方法中。事实上,这就是Rails对
pluralize
的作用。我们可以创建自己的简化版本:

def pluralize(count, noun)
  "#{count} #{count==1 ? noun : noun + 's'}"
end
那么您的代码可能如下所示:

99.downto(1) do |n|
  puts "#{pluralize(n, "bottle")} of beer on the wall, #{pluralize(n, "bottle")} of beer."
  puts "Take one down and pass it around, #{pluralize(n-1, "bottle")} of beer on the wall."
end

问:你确定那一刻你真的会降到“1”吗?你应该在减量后移动三元数吗?你在2停止while循环。减去一后,不会重新计算复数或单数。问:你确定你当时真的降到了“1”吗?你应该在减量后移动三元数吗?你在2停止while循环。减去一后,不会重新计算复数或单数。你应该再往下走。第一次检查现在完全没有必要了。啊,是的!出于某种原因,我假设每次插值时都会计算出
复数或单数。呜呜!谢谢你,马克!第一次检查现在完全没有必要了。啊,是的!出于某种原因,我假设每次插值时都会计算出
复数或单数。呜呜!谢谢你,马克!哎呀!使用
.downto
和自定义方法会更干净,谢谢!哎呀!使用
.downto
和自定义方法会更干净,谢谢!