In his excellent book Effective Java, Josh Bloch talks a lot about the 
benefits of making value objects immutable, and about objects being 
"good citizens", by being in a consistent and usable state immediately 
after construction (rather than calling a bunch of setter methods to set 
up the state before you can use the instance).

A string in Ruby seems to me to be just a convenient way of manipulating 
a char array, with a bunch of useful methods such as regular expressions 
thrown in. It should be treated no more "specially" than any other class 
backed by an array. As Ged says, Ruby has the whole immutable value 
object model sewn up much more consistently anyway, with the .freeze 
method. You can make anything immutable whenever you like - including in 
the initialize method of a well-behaved value object.

Looks to me like another victory for matz's commitment to 
non-astonishing behaviour - I don't have to learn which objects are 
constructed as immutable by default! I'm liking this language more and 
more every day.

Now where's that "final" keyword..?

Cheers,
Dan

Ged Byrne wrote:

>Am I correct in thinking that as long as you avoid the
>methods ending with an exclamation mark, then your
>strings will be effectively immutable?
>
>
> --- Robert Feldt <feldt / ce.chalmers.se> wrote: > 
>  
>
>>>|www.artima.com in which he criticized Ruby's
>>>      
>>>
>>mutable strings - saying
>>    
>>
>>>|that of such a horror he could ne'er conceive, or
>>>      
>>>
>>something :)
>>    
>>
>>>If mutable strings are horror, mutable
>>>      
>>>
>>dictionaries should be as well.
>>    
>>
>>>The one with such standard should go to
>>>      
>>>
>>side-effect free language like
>>    
>>
>>>Haskell. ;-)
>>>
>>>      
>>>
>>I agree that it sounds like a very ad hoc design
>>decision.
>>
>>    
>>
>>>I rarely have such problems caused by mutable
>>>      
>>>
>>strings.  Besides, Ruby
>>    
>>
>>>is not a language to keep people away from horror.
>>>      
>>>
>> You can write
>>    
>>
>>>ugly, scary, or dangerous programs in Ruby, if you
>>>      
>>>
>>want.  It's cost
>>    
>>
>>>for freedom.
>>>
>>>      
>>>
>>This points to an important "philosophical"
>>difference that stands
>>out to me when comparing Ruby and Python: Python
>>gives me the feeling of 
>>trying to be a "Language for the Masses" (LFM) and
>>tries to put 
>>restrictions on what can be done. You need to format
>>your code in a certain 
>>way, your strings (but only a minority of other
>>objects) are immutable by 
>>default etc. As discussed on ll1-discuss mailing
>>list and
>>other places LFM's are in contrast to LFSP's
>>(Language for Smart People /
>>experts) that tries to give you as much power as
>>possible.
>>
>>To me the LFM thinking is absurd and even inhumane
>>since it (ironically
>>enough) says that there is an elite that know the
>>area well enough to
>>decide what other people is allowed to use.
>>
>>To me matz goal seems to be for less of an LFM than
>>Guidos.
>>
>>Then again I have too little experience with Python
>>vs. Ruby to actually
>>know. I also may be exaggerating since also matz has
>>his limits (no
>>macros in Ruby, no multi-methods etc) ;).
>>
>>Regards,
>>
>>Robert Feldt
>>
>>
>>Ps. "Rough" Immutable strings in ruby:
>>
>>s1 = "a"
>>s1[0] = ?b
>>p s1              # => b
>>def ims(string)
>>  string.freeze
>>end
>>s2 = ims "a"
>>p s1              # => a
>>s2[0] = ?b        # => `[]=': can't modify frozen
>>string (TypeError)
>>
>>;)
>>
>> 
>>    
>>
>
>________________________________________________________________________
>Want to chat instantly with your online friends?  Get the FREE Yahoo!
>Messenger http://uk.messenger.yahoo.com/
>
>
>  
>