各ブロックをインジェクトに変換するためにリファクタリングを実行しようとしましたが、機能せず、理由がわかりません。
リファクタリングの前に機能するコードは次のとおりです。
class String
# Build the word profile for the given word. The word profile is an array of
# 26 integers -- each integer is a count of the number of times each letter
# appears in the word.
#
def profile
profile = Array.new(26) { 0 }
self.downcase.split(//).each do |letter|
# only process letters a-z
profile[letter.ord - 'a'.ord] += 1 unless letter.ord > 'z'.ord
end
profile
end
end
動作しないリファクタリングは次のとおりです。
class String
# Build the word profile for the given word. The word profile is an array of
# 26 integers -- each integer is a count of the number of times each letter
# appears in the word.
#
def profile
self.downcase.split(//).inject(Array.new(26) {0}) do |profile, letter|
# only process letters a-z
profile[letter.ord - 'a'.ord] += 1 unless letter.ord > 'z'.ord
end
end
end
リファクタリングされたメソッドを実行しようとすると、
`block in profile': undefined method `[]=' for 1:Fixnum (NoMethodError)
それを正しく理解していれば、リファクタリングされたバージョンのプロファイルオブジェクトの配列参照演算子とは異なります。これは、injectに渡されたイニシャライザーが機能していないことを意味します。その理解は正しいですか?もしそうなら、なぜですか?
ありがとう!