2012-12-09 24 views
0

我不知道为什么这不是在rails中工作,但它在IRB中工作。设置一个现有的变量到自己的第一个元素

我做这样的事情:

response = response.first 

response是哈希数组。

在irb中,当试图模拟它时,它工作正常。

如:

如:

(rdb:1) response = response.first 
NoMethodError Exception: undefined method `first' for nil:NilClass 
(rdb:1) response 
nil 

然后

>> a = [{'a'=>3}] 
=> [{"a"=>3}]  
>> a = a.first 
=> {"a"=>3}  

然而,在调试模式下,黄瓜(在我的步骤定义),我做了上述语句时得到这个,response设置为nil

为什么行为不同?

回答

1

您是否100%确定response局部变量?如果response是一个方法这可以解释你所看到的行为:局部变量response阴影方法response。如果你要拨打的response方法,你需要明确地告诉红宝石希望的方法,通过提供任何参数列表或接收器:

response = response().first 
# or 
response = self.response.first 
+0

正确的金钱,:) ..我想通了和反应是一个方法在不同的文件中。 D'哦.. – volk

相关问题