From: Robert Klemme Date: 2009-04-16T18:45:47+09:00 Subject: Re: Tokenizing a large file 2009/4/16 Ryan Davis : > > On Apr 16, 2009, at 02:27 , Robert Klemme wrote: > >> Converted to the block form: >> >> def my_tokenize file >>  tokens = Hash.new {|h,k| h[k.freeze] = k} > > FYI: > > % irb >>> h = {} > => {} >>> h["key"] = 42 > => 42 >>> h.keys.map { |k| k.frozen? } > => [true] > > hashes dupe and freeze string keys to prevent them from being mutated while > hash keys. Only if they are not frozen yet. irb(main):001:0> h = {} => {} irb(main):002:0> s = "abc" => "abc" irb(main):003:0> h[s] = s => "abc" irb(main):004:0> s = "bar".freeze => "bar" irb(main):005:0> h[s] = s => "bar" irb(main):006:0> h => {"abc"=>"abc", "bar"=>"bar"} irb(main):007:0> h.each {|kv| p kv.map {|x| x.object_id}} [134954550, 134972840] [134951170, 134951170] => {"abc"=>"abc", "bar"=>"bar"} Do you now know why I did it the way I did? Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/