gpt4 book ai didi

Ruby if vs 行尾 if 行为不同?

转载 作者:数据小太阳 更新时间:2023-10-29 06:36:49 25 4
gpt4 key购买 nike

为什么这段代码不起作用?

b if b = true

错误:未定义局部变量或方法“b”

但是这样做:

if b = true
b
end

他们不应该是一样的吗?

最佳答案

这是一个很好的问题。它与 Ruby 中变量的作用域有关。

这是一个post by Matz on the Ruby bug tracker关于这个:

local variable scope determined up to down, left to right. So a local variable first assigned in the condition of if modifier is not effective in the left side if body. It's a spec.

关于Ruby if vs 行尾 if 行为不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11022800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com