From: Robert Klemme Date: 2007-09-19T06:55:03+09:00 Subject: Re: Modifying a hash key On 18.09.2007 20:05, Alex Shulgin wrote: > On Sep 17, 10:41 pm, Robert Klemme wrote: >> On 17.09.2007 20:19, Alex Shulgin wrote: >> >>>> I'd do this, note all the freezing in order to make errors with >>>> changing keys obvious. >>>> state = Hash.new {|h,k| h[k]=[]} >>>> prefix = [].freeze >>>> ARGF.each do |line| >>>> line.scan /\w+/ do |word| >>>> state[prefix] << word.freeze >>>> (prefix += [word]).freeze >>>> # or: prefix = (prefix.dup << word).freeze >>>> end >>>> end >>> Uh-oh... this freeze stuff seems overly complicated to me. >> Well, it's not necessary - I just put it there in order to find bugs. >> > [snip] >> You find my code "overly complicated"? Amazing... > > Oh, sorry, I didn't want to hurt anyone... Not hurt, just astonished. :-) > First of all your and mine code do different things Yes and no: your code does more but as far as I can see the gathering does basically the same in different ways. >, and most > importantly that freeze stuff _really_ scared me. I thought it was > some kind of garbage-collection voodoo. ;-) No, it just prevents changing an instance. #freeze has nothing to do with GC (unless you count not being able to overwrite a reference to an instance with a reference to nil). > Now I see it may be safely removed after debugging the code. This way > your code looks much better, thanks! :-) Cheers robert