>> animals = Hash.new { |hash, key| hash[key] = [] } => {} >> animals[:dogs] << "Mira" => ["Mira"] >> animals.default_proc => #<Proc:0x00320fdc@(irb):9>
Let’s switch back to a less funky default value:
>> animals.default = nil => nil >> animals[:bugs] => nil
No, no, funky is cool!
>> animals.default_proc = proc { |hash, key| hash[key] = [] } NoMethodError: undefined method `default_proc=' for {:dogs=>["Mira"]}:Hash
What? Can anyone tell me why this doesn’t exists? It’s so simple to add!
class Hash def default_proc=(proc) initialize(&proc) end end