MultiMap

I wanted to create a MultiMap so for a key it stores not a single value
but an Array. The code is like that:

class MultiMap < Hash

def store (key, value)
if !super[key]
dat = Array.new
super[key]=dat #*
end
super[key].push(value)
end

For some reason it doesn’t work. I found by logging that even after line

  • if I call super[key] I still get nil. What’s wrong with the code?

Hi,

···

At Mon, 2 Dec 2002 10:03:13 +0900, Roman Rytov wrote:

def store (key, value)
if !super[key]
dat = Array.new
super[key]=dat #*
end
super[key].push(value)
end

For some reason it doesn’t work. I found by logging that even after line

  • if I call super[key] I still get nil. What’s wrong with the code?

`super’ invokes the same name method of superclass.

def store(key, value)
(self[key] ||= ).push(value)
end


Nobu Nakada