Discussion: Modifying a hash key
Afficher un message
Vieux 18/09/2007, 19h05   #7
Alex Shulgin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Modifying a hash key

On Sep 17, 10:41 pm, Robert Klemme <shortcut...@googlemail.com> 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...

First of all your and mine code do different things, and most
importantly that freeze stuff _really_ scared me. I thought it was
some kind of garbage-collection voodoo. ;-)

Now I see it may be safely removed after debugging the code. This way
your code looks much better, thanks!


Alex

  Réponse avec citation
 
Page generated in 0,04685 seconds with 9 queries