0
使用Ruby 1.9.2,我希望我的DictionaryPresenter
類擴展Enumerable
,但我在each
方法中出現錯誤。錯誤是dictionary
是nil
,即使它在initialize
中正確分配。爲什麼這個實例變量忽然有nil的值?
我認爲這與使用dictionary
的屬性方法有關,而不是直接使用實例變量@dictionary
。我讀過你應該儘可能地用屬性方法替換實例變量的使用,這就是我所做的。
class DictionaryPresenter
include Enumerable
attr_accessor :dictionary
private :dictionary
def initialize(collection)
dictionary = dictionary_hash
collection.each do |element|
dictionary[element[0].capitalize] << element
end
p 'dictionary has been filled. it is'
p dictionary
end
def dictionary_hash
('A'..'Z').reduce({}) do |hash, letter|
hash[letter] = []
hash
end
end
def each(&blk)
p 'in each'
p dictionary
dictionary.each(&blk)
end
end
p DictionaryPresenter.new(['abba', 'beegees']).map{ |a| a }
輸出
"dictionary has been filled. it is"
{"A"=>["abba"], "B"=>["beegees"], "C"=>[], "D"=>[], "E"=>[], "F"=>[], "G"=>[], "H"=>[], "I"=>[], "J"=>[], "K"=>[], "L"=>[], "M"=>[], "N"=>[], "O"=>[], "P"=>[], "Q"=>[], "R"=>[], "S"=>[], "T"=>[], "U"=>[], "V"=>[], "W"=>[], "X"=>[], "Y"=>[], "Z"=>[]}
"in each"
nil
anki.rb:22:in `each': undefined method `each' for nil:NilClass (NoMethodError)
from anki.rb:25:in `map'
from anki.rb:25:in `<main>'
謝謝,就是這樣!我試圖不使用實例變量來重載它! – ben
你能解釋一下你的意思嗎?「在類方法中的所有變量賦值都是這樣。」?也許值得注意的是,另一種表示它是setter的方法是'send(:dictionary =,dictionary_hash)',或者你可以直接引用實例變量('@dictionary = ..')。一個小點:初始化只是一個常規方法,而不是構造函數('new'可能不是,但它更接近)。 –