[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: string freeze

Rick DeNatale

4/24/2007 12:24:00 AM

On 4/23/07, Stephen Smith <4fires@gmail.com> wrote:
> Hey all,
>
> So I'm trying to freeze a class variable, and I discover that Object#freeze
> breaks down every time on my machine. Even with simple strings.
>
> irb(main):001:0> test = "good"
> => "good"
> irb(main):002:0> test.freeze
> => "good"
> irb(main):003:0> test.frozen?
> => true
> irb(main):004:0> test = "bad"
> => "bad"
> irb(main):005:0> test.frozen?
> => false
>
> What gives?
>
> I must be missing something.

You are missing the difference between a variable and an object.

test = "good"

test is a local variable which references a string object.

test.freeze?

This freezes the object referenced by test.

test = "bad"
this makes the variable test refer to an entirely different string.

For another thing to ponder try this:

test = "good"
test.freeze

another_variable = test
another_variable.frozen? => true

The point is that you can't freeze variables.

This distinction between variables and the objects they reference is
a stumbling block for many people coming to Ruby if they haven't been
exposed to other languages in the family.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...