Hi,
In message "[ruby-talk:4614] Re: Getting list of regexp matches"
on 00/08/28, Dave Thomas <Dave / thomases.com> writes:
|Currently the default parameter is evaluated just once, so all entries
|will default to having the same empty array.
|
| h = Hash.new(Array.new)
| h['cat'] << 'hello'
| h['dog'] << 'goodbye'
| puts h['cat'].inspect #=> ["hello", "goodbye"]
|
|
|However, there's some talk of a new syntax:
|
| h = Hash.new { stuff }
|
|Where the block will be evaluated to create missing elements.
Or try
h.fetch('cat'){ stuff }
if you're using 1.5.x or later.
matz.