Forum: Ruby string freeze

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E87bf52b19c95ec977aba1c0c777f752?d=identicon&s=25 Stephen Smith (Guest)
on 2007-04-24 02:10
(Received via mailing list)
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.

fiery
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2007-04-24 02:25
(Received via mailing list)
On 4/23/07, Stephen Smith <4fires@gmail.com> wrote:
> => 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.denhaven2.com/
4371f8fa1c3574e74d8f88c77d23e7d7?d=identicon&s=25 Harry Ha (harry)
on 2007-04-24 03:06
(Received via mailing list)
On 4/24/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.
>
>
> fiery
>

Try altering it instead of reassigning it.
I think this is a little more like what you expected.
Not exactly, but you can see freezing.


test = "good"
p test
test.freeze
p test
p test.frozen?

test << "bad"
p test
p test.frozen?

Harry
7914035d29e46e9207a81f2c52f9f96f?d=identicon&s=25 Abhijit Gadgil (Guest)
on 2007-04-24 05:49
(Received via mailing list)
test references two different objects.. try this....

test = "good"
test.freeze
test.object_id

test = "bad"
test.object_id

You will notice different object_id's in the first and later case.

-abhijit

On 4/24/07, Harry <ruby.hardware@gmail.com> wrote:
> Try altering it instead of reassigning it.
> test << "bad"
> p test
> p test.frozen?
>
> Harry
>
> --
> http://www.kakueki.com/ruby/list.html
> A Look into Japanese Ruby List in English
>
>


--

अभिजीत

[written in http://www.paahijen.com/scratchpad]

[http://www.paahijen.com]
This topic is locked and can not be replied to.