From: George Ogata Date: 2006-03-10T04:33:43+09:00 Subject: Re: [Newbie] Immutable objects Einar writes: > Robert Klemme wrote: >> Einar H�st wrote: >> It's not sufficient, you'll have to freeze those strings, too. Note >> that >> this will freeze the originals which probably is a bad idea. You could do >> def ini(suit, rank) >> @suit = suit.dup.freeze >> @rank = rank.dup.freeze >> freeze >> end >> > > Ah, I see - in C# "where I come from", strings are immutable. Of > course, no-one will be using this code except me, so I'm definitely > conjuring imaginary problems here - but at the same time, the whole > point of this project is to learn about the language :-) For things like @suit -- which I take it can only take one of four enumerated values -- I think it's actually more rubyesque to use symbols. e.g.: @suit = :spade This has the side benefit that Symbols themselves are effectively immutable. But as Robert and James hinted, freezing is seldom used. It's like type-checking: you can be a complete nazi about banning anything out of line, but (a) you lose elegance and simplicity of code and (b) your tests should probably be checking this anyway. YMMV.