Freeze string fails to freeze?


#1

a=‘asdf’
=> “asdf”

a.freeze
=> “asdf”

a=‘qwer’
=> “qwer”

should’nt that throw and exception ???


#2

On 15.03.2007 16:33, removed_email_address@domain.invalid wrote:

a=‘asdf’
=> “asdf”

a.freeze
=> “asdf”

a=‘qwer’
=> “qwer”

should’nt that throw and exception ???

No, the object is frozen. This has no effects on the variable.

a=‘asdf’
=> “asdf”

a.freeze
=> “asdf”

a << “foo”
TypeError: can’t modify frozen string
from (irb):4:in `<<’
from (irb):4
from :0

Kind regards

robert


#3

Le jeudi 15 mars 2007 16:35, removed_email_address@domain.invalid a écrit :

=> “qwer”

should’nt that throw and exception ???

No, because calling the freeze method prevents from modifying the
object, it
doesn’t prevent from modifying the variable. Remember that a variable is
just
a way to give a name to an object, but it is not tied to this object
forever.

So, your code doesn’t raise an exception, but this one does :

irb(main):001:0> a=“abc”
=> “abc”
irb(main):002:0> a.freeze
=> “abc”
irb(main):003:0> a << ‘d’
TypeError: can’t modify frozen string
from (irb):3:in `<<’
from (irb):3

What you are looking for is closer of a constant, actually :

irb(main):004:0> A=“abc”
=> “abc”
irb(main):005:0> A=“def”
(irb):5: warning: already initialized constant A


#4

removed_email_address@domain.invalid wrote:

a=‘asdf’
=> “asdf”

a.freeze
=> “asdf”

a=‘qwer’
=> “qwer”

should’nt that throw and exception ???

freeze is a method on the object referenced by a,
not on the variable a (a is just a name, not the
object itself).

a = ‘abcd’
a.freeze

a[2]=‘w’ raises

./freezetest.rb:7:in `[]=’: can’t modify frozen string (TypeError)

This causes an exception because you are trying to modify the
frozen object.

a = ‘defg’

This does not, because the object (which up to now was
referenced by a) is not changed. a now refers to a
different object.

Try it with a constant:

A = ‘abcd’
A = ‘defg’ # raises a warning: already initialized constant A

Regards,

Fergal Byrne - Technical Director

Adnet: Web Builders to the Design Industry

http://www.adnet.ie/ t:+353 1 855 8951 aim/skype:FergByrne

=== We’ve Moved! 63 Lower Gardiner Street, Dublin 1 ===


#5

Le jeudi 15 mars 2007 23:16, Rick DeNatale a écrit :

On 3/15/07, Olivier R. removed_email_address@domain.invalid wrote:

No, because calling the freeze method prevents from modifying the object,
it doesn’t prevent from modifying the variable. Remember that a variable
is just a way to give a name to an object, but it is not tied to this
object forever.

Is it just me or has the confusion between variables and objects been
coming up more frequently than usual of late?

Not more often than the elsif/elseif confusion :smiley:


#6

On 3/15/07, Olivier R. removed_email_address@domain.invalid wrote:

No, because calling the freeze method prevents from modifying the object, it
doesn’t prevent from modifying the variable. Remember that a variable is just
a way to give a name to an object, but it is not tied to this object forever.

Is it just me or has the confusion between variables and objects been
coming up more frequently than usual of late?


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/