I have an object that has attributes name and data, among others. I want to create a hash that uses the name as the key and the data (which is an array) as the value. I can't figure out how to reduce the code below using map. Is it possible?
def fc_hash
fcs = Hash.new
self.forecasts.each do |fc|
fcs[fc.name] = fc.data
end
fcs
end
mapreturns an array, and will not return a hash. But that said, there isn't really anything wrong with this code. Why not use it just like you have it?forecasts? Particularly, what is its class?Forecastinstances, doesn't it?{ }is almost always preferable toHash.new. The only reason to call the formal constructor is for passing in defaults, likeHash.new(0)orHash.new { |h,k| ... }.