From: Robert Klemme Date: 2009-03-01T19:46:27+09:00 Subject: Re: Newbie question > Hash workings 2009/3/1 AD60 : > Thank you both for seeing the light at the end of the tunnel. I fully > understand what I have to do to get it working but just to clarify; Good! > By doing "h[2] << 20" I modified the original empty array I set as a > default value and inserted no hash key because it is not an assignment > to h? Yes. Although "assignment to h" is a bit ambiguous. I guess you meant the correct thing but keep in mind that normally "assignment to h" means "h = something" and not "h[key] = something". The latter is actually a method defined in class Hash. > Replacing the "h = Hash.new([])" with "h = Hash.new { |h,k| h[k] = > []}" and just doing "p h[2]" before any assigment to h[2] has the > surprising/logical side effect of creating a new key "2", thank god > with value "[]" so no problems there, but still... I am now thinking > that stating "h = Hash.new([])" is problematic idiom under all > circumstances. Am I correct? No, because it works well in other cases - the most popular probably being counters = Hash.new 0 ... counters[foo] += 1 Kind regards robert -- remember.guy do |as, often| as.you_can - without end