2012-12-09 25 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

相關問題