2013-04-04 70 views
0

我試圖訪問一個字符串數組在我的應用程序,並收到錯誤初始化回調字符串數組中的Rails

undefined method `each' for nil:NilClass 

我回到我的代碼,並發現,無論我做什麼,當我試圖訪問字符串數組,對象不斷返回nil:

class Foo 
    attr_reader :bar 
    def initalize 
    @bar = ['a','b','c'] 
    end 
end 

> a = Foo.new 
=> #<KeywordsController::Foo:0x007fa3541b3920> 
> a.bar 
=> nil 

如何創建一個類在那裏我可以訪問字符串數組?

+1

你拼錯'初始化' – 2013-04-04 19:58:13

回答

3
class Foo 
    attr_reader :bar 
    def initialize 
    @bar = ['a','b','c'] 
    end 
end 

其中一個令人瘋狂的拼寫錯誤,你不能看到,而尋找更多的怪物和怪異的問題!

+0

在foobar的例子中,它應該只用'attr_reader'工作。你剛剛拼錯'initialize'。 – 2013-04-04 20:02:29