From: Farhad Farzaneh Date: 2007-07-05T01:42:53+09:00 Subject: Re: Isn't hash default value behavior just a little bizarre? unknown wrote: > Hi -- > > > All you're doing is assigning an object to a certain role. There's no > implication of automatic duplication of the object. I was thinking of it as initialization of the value of the item accessed, but when you think of it as *the* default object it makes sense. > >> I got stung when I was actually >> using the feature to initialize any entry to an array just to avoid the >> business of >> >> hash[key] ||= [] > > It doesn't do that anyway: > > h = {} > h.default = [] > p h["blah"] # [] (default value for undefined key) > p h # {} (no keys are defined) > > The default value is for undefined keys, whereas hash[key] ||= [] > actually sets a key. > The misunderstanding on my part was that I thought it initialized the key to the default object, rather than returned the actual default object. In this context, if the key is automatically initialized to an empty array, then when I wanted to assign something to it, I wouldn't have to first make it an array. -- Posted via http://www.ruby-forum.com/.