From: Robert Klemme Date: 2008-12-03T00:33:17+09:00 Subject: Re: Adventures in Optimization... or why CONST frozen is Good 2008/12/2 Brian Candler : >> Now I have a class CONSTANT... >> UNDEFINED = 'U'.freeze >> >> and >> symbols_needed[symbol_name] = UNDEFINED > > Yes, that's the "right" solution with Ruby today, and you'll see this > done in a lot of Ruby libraries. (Perhaps it would be nice if there were > some syntax to define an inline frozen string literal) > > I don't consider this any sort of "optimisation" though. It's > fundamental to the nature of Ruby that there is only one kind of value, > which is a reference to an object. An assignment always copies only the > reference. Completely agree, this comes as no surprise. Actually, this is an obvious design decision, if you want to use the same value to denote a particular state then just use one object. Another, probably more subtle issue is this: irb(main):001:0> s="foo" => "foo" irb(main):002:0> h={s=>1} => {"foo"=>1} irb(main):003:0> s.equal? h.keys.first => false irb(main):004:0> [s.object_id, h.keys.first.object_id] => [1073539250, 1073539280] irb(main):005:0> s.freeze => "foo" irb(main):006:0> h={s=>1} => {"foo"=>1} irb(main):007:0> s.equal? h.keys.first => true irb(main):008:0> [s.object_id, h.keys.first.object_id] => [1073539250, 1073539250] In other words, there is a hidden dup going on if the Hash key is a String which is not frozen. Kind regards robert -- remember.guy do |as, often| as.you_can - without end