James Gray
3/9/2006 2:49:00 PM
On Mar 9, 2006, at 6:23 AM, Einar Høst wrote:
> Hi,
>
> Still working on the basics... for my card game, I want the Card
> objects to be immutable. Is it sufficient to put 'freeze' in the
> end of the constructor, like this (suit and rank will be strings):
>
> def initialize(suit, rank)
> @suit, @rank = suit, rank
> freeze
> end
As a Ruby rule of thumb, you make an immutable object just by
providing no methods to change the instance data.
Technically, there are ways around that, but there are also ways
around freeze() . Don't lose a lot of sleep over these though. If
people don't follow your API, they know they are breaking the rules
and they are responsible for the consequences.
James Edward Gray II