From: Brian Candler Date: 2008-12-02T22:52:17+09:00 Subject: Re: Adventures in Optimization... or why CONST frozen is Good > 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. This is a breath of fresh air when compared to, say, Perl. Is this value a scalar? Is it a scalar number or string, or a reference to an Array or a Hash, or a typeglob, or a filehandle, or ...? However, you could argue that string literals should have been immutable (like Symbol). The language would end up being somewhat different to use: a = "hello" # maybe Symbol or StringLiteral b = String.new(a) # mutable String b << " world" You'd also have to have a load of rules to work out. Should a.dup return the same Symbol, or a new mutable String? Should (a + "world") return a new Symbol, or a new mutable String? From this point of view, just having String keeps things simple, even if it does end up creating a load of garbage objects. In those cases where this matters, your approach (of profiling and zapping) is a good one. -- Posted via http://www.ruby-forum.com/.