Freeze method doesn't work

Freeze should prevent further modifications to 'var'...

mac-undrgrnd:~ yspro$ irb
ruby-1.9.2-p0 > var = 4
=> 4
ruby-1.9.2-p0 > var.frozen?
=> false
ruby-1.9.2-p0 > var.freeze
=> 4
ruby-1.9.2-p0 > var.frozen?
=> true
ruby-1.9.2-p0 > var = 5
=> 5
ruby-1.9.2-p0 > var.frozen?
=> false
ruby-1.9.2-p0 >

What's wrong?

···

--
Posted via http://www.ruby-forum.com/.

Variables reference objects. It's the object you can freeze, not the
variable. If you make the variable point to a different object, the
original one is still frozen, but the new one maybe not.

Jesus.

···

On Thu, Oct 14, 2010 at 5:00 PM, Sergey U. <2ys.pro@gmail.com> wrote:

Freeze should prevent further modifications to 'var'...

mac-undrgrnd:~ yspro$ irb
ruby-1.9.2-p0 > var = 4
=> 4
ruby-1.9.2-p0 > var.frozen?
=> false
ruby-1.9.2-p0 > var.freeze
=> 4
ruby-1.9.2-p0 > var.frozen?
=> true
ruby-1.9.2-p0 > var = 5
=> 5
ruby-1.9.2-p0 > var.frozen?
=> false
ruby-1.9.2-p0 >

What's wrong?

Freeze should prevent further modifications to 'var'...

mac-undrgrnd:~ yspro$ irb
ruby-1.9.2-p0 > var = 4
=> 4
ruby-1.9.2-p0 > var.frozen?
=> false
ruby-1.9.2-p0 > var.freeze
=> 4
ruby-1.9.2-p0 > var.frozen?
=> true
ruby-1.9.2-p0 > var = 5
=> 5
ruby-1.9.2-p0 > var.frozen?
=> false
ruby-1.9.2-p0 >

What's wrong?

The freeze method doesn't operate on var itself. It operates on the
object that var references, so the Fixnum object for 4 was frozen in
your test. However, you are allowed to replace that object with another
any time you please. The freeze method only prevents you from changing
the object itself:

irb(main):001:0> var = 'abc123'
=> "abc123"
irb(main):002:0> var.frozen?
=> false
irb(main):003:0> var.freeze
=> "abc123"
irb(main):004:0> var.frozen?
=> true
irb(main):005:0> var.sub!(/\d/, 'n')
TypeError: can't modify frozen string
        from (irb):5:in `sub!'
        from (irb):5
irb(main):006:0> var
=> "abc123"
irb(main):007:0> var = 'abcn23'
=> "abcn23"
irb(main):008:0> var.frozen?
=> false

-Jeremy

···

On 10/14/2010 10:00 AM, Sergey U. wrote:
        from :0

No. freeze prevents modifications to the object contained in var. For example:

str = "hello"
str.upcase!
puts str
=> HELLO
str.freeze
str.downcase!
=> RuntimeError: can't modify frozen string

str1 = str
str1.downcase!
=> RuntimeError: can't modify frozen string

Note that freeze is useless on classes (like Integer or Symbol) which don't
have destructive methods.

The nearest you can come to prevent modifications to a variable is to use a
constant instead (but remember that ruby allows you to assign a different
value to a constant, it just emits a warning). However, the object the
constant contains can be freely modified:

CONST="hello"
CONST.upcase!
puts CONST
"HELLO"
CONST="bye"
=> warning: already initialized constant CONST
puts CONST
=> "bye"

If you also want to prevent modifications to the object in the constant,
freeze it.

I hope this helps

Stefano

···

On Thursday 14 October 2010, Sergey U. wrote:

>Freeze should prevent further modifications to 'var'...

Thank you Jeremy a lot!

···

--
Posted via http://www.ruby-forum.com/.