在ruby中使用"and"时,地球是平的
When using "and" in ruby, the earth is flat
正如我在逻辑方面一直被教导的那样,“and”运算符意味着两个值都必须为真,整个语句才为真。如果你有许多用“和”链接的陈述,那么其中任何一个是错误的应该使整个声明都是错误的。然而,在 Ruby 中,我 运行 进入了这个场景:
horizon_flat = true
one_up_and_down = true
magellan_fell = false
flat_earth_thesis = horizon_flat and one_up_and_down and magellan_fell
puts("Hey ruby, doesn't the horizon look flat?")
puts(horizon_flat) # true
puts("Isn't there only one up and one down?")
puts(one_up_and_down) # true
puts("Did Magellan fall off the earth?")
puts(magellan_fell) # false
puts("Is the earth flat?")
puts(flat_earth_thesis) # true
St运行gely,如果我只是 运行 声明本身,它 returns 正确 puts(horizon_flat and one_up_and_down and magellan_fell) # false
但是如果我将该语句存储在一个变量中,然后调用它,该变量会输出 true。为什么ruby认为地球是平的?
当你期待这个时:
flat_earth_thesis = horizon_flat and one_up_and_down and magellan_fell
待评价为:
flat_earth_thesis = (horizon_flat and one_up_and_down and magellan_fell)
它被评估为:
(flat_earth_thesis = horizon_flat) and one_up_and_down and magellan_fell
如评论中所述,请查看 operator precedence。
正如我在逻辑方面一直被教导的那样,“and”运算符意味着两个值都必须为真,整个语句才为真。如果你有许多用“和”链接的陈述,那么其中任何一个是错误的应该使整个声明都是错误的。然而,在 Ruby 中,我 运行 进入了这个场景:
horizon_flat = true
one_up_and_down = true
magellan_fell = false
flat_earth_thesis = horizon_flat and one_up_and_down and magellan_fell
puts("Hey ruby, doesn't the horizon look flat?")
puts(horizon_flat) # true
puts("Isn't there only one up and one down?")
puts(one_up_and_down) # true
puts("Did Magellan fall off the earth?")
puts(magellan_fell) # false
puts("Is the earth flat?")
puts(flat_earth_thesis) # true
St运行gely,如果我只是 运行 声明本身,它 returns 正确 puts(horizon_flat and one_up_and_down and magellan_fell) # false
但是如果我将该语句存储在一个变量中,然后调用它,该变量会输出 true。为什么ruby认为地球是平的?
当你期待这个时:
flat_earth_thesis = horizon_flat and one_up_and_down and magellan_fell
待评价为:
flat_earth_thesis = (horizon_flat and one_up_and_down and magellan_fell)
它被评估为:
(flat_earth_thesis = horizon_flat) and one_up_and_down and magellan_fell
如评论中所述,请查看 operator precedence。