相关疑难解决方法(0)

如果"if"语句位于子句的末尾,为什么ruby中的条件语句和赋值会失败?

为什么最后一个语句(语句末尾的"if(tmp2 = foo)")失败?

def foo;5;end

# this one works
if (tmp = foo)
  puts tmp.to_s
end

# why this one fails
puts tmp2.to_s if (tmp2 = foo) #=> undefined local variable or method ‘tmp2’ for main:Object
Run Code Online (Sandbox Code Playgroud)

ruby

5
推荐指数
1
解决办法
1370
查看次数

标签 统计

ruby ×1