NameError:未定义-在Ruby 2.1.2中是否更改了局部变量的解析规则? [英] NameError: undefined - have parsing rules for local variables changed in Ruby 2.1.2?

查看:83
本文介绍了NameError:未定义-在Ruby 2.1.2中是否更改了局部变量的解析规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用ruby 2.1.2获取NameError: undefined local variable or method

I am getting NameError: undefined local variable or method with ruby 2.1.2

中观察到的>这个问题,像这样的表达式:

bar if bar = true

引发未定义的局部变量错误(前提是未事先定义bar),因为解析器在分配bar之前会读取它.而且我相信,此表达式与以前没有什么不同:

raises an undefined local variable error (provided that bar is not defined prior) because bar is read by the parser before it is assigned. And I believe that there used to be no difference with that with this expression:

bar if bar = false

两者之间的区别在于是否对主体进行了评估,但是如果在遇到条件之前遇到未定义的局部变量会立即引发错误,则无关紧要.

The difference between the two is whether the main body is evaluated or not, but that should not matter if encountering an undefined local variable immediately raises an error before evaluating the condition.

但是当我在Ruby 2.1.2上运行第二个代码时,它不会引发错误.以前是那样吗?如果是这样,那么解析讨论的全部内容是什么?如果没有,是否更改了Ruby规范?有什么参考吗?它在1.8.7、1.9.3等中做了什么?

But when I run the second code on Ruby 2.1.2, it does not raise an error. Has it been like that from before? If so, then what was the parsing discussion all about? If not, has Ruby specification changed? Is there any reference to that? What did it do in 1.8.7, 1.9.3, etc. ?

推荐答案

是的,在ruby 2.1.2中已更改

1.8.71.9.32.0.0甚至2.1.1中,我收到2条警告,没有错误:

Yes it changed in ruby 2.1.2

In 1.8.7, 1.9.3, 2.0.0 and even 2.1.1 I get 2 warnings and no errors:

2.0.0-p247 :007 > bar if bar = false
(irb):7: warning: found = in conditional, should be ==
 => nil 
2.0.0-p247 :008 > bar if bar = true
(irb):8: warning: found = in conditional, should be ==
 => true 

在您提到的2.1.2版本中,我会收到2条警告和1条NameError错误.

whereas in the 2.1.2 version you mention I get 2 warnings and 1 NameError error.

2.1.2 :001 > bar if bar = true
(irb):1: warning: found = in conditional, should be ==
NameError: undefined local variable or method `bar' for main:Object
        from (irb):1
        from /home/durrantm/.rvm/rubies/ruby-2.1.2/bin/irb:11:in `<main>'
2.1.2 :002 > bar if bar = false
(irb):2: warning: found = in conditional, should be ==
 => nil 

这是在我的Ubuntu 14上

This is on my Ubuntu 14

这篇关于NameError:未定义-在Ruby 2.1.2中是否更改了局部变量的解析规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆