Freeze string fails to freeze?

a='asdf'

=> "asdf"

a.freeze

=> "asdf"

a='qwer'

=> "qwer"

should'nt that throw and exception ???

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

Kind regards

  robert

···

On 15.03.2007 16:33, jwaala.amplify@gmail.com wrote:
         from :0

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

···

Le jeudi 15 mars 2007 16:35, jwaala.amplify@gmail.com a écrit :

>> a='asdf'

=> "asdf"

>> a.freeze

=> "asdf"

>> a='qwer'

=> "qwer"

should'nt that throw and exception ???

--
Olivier Renaud

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

···

jwaala.amplify@gmail.com wrote:

a='asdf'

=> "asdf"

a.freeze

=> "asdf"

a='qwer'

=> "qwer"

should'nt that throw and exception ???

--

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 ===

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

···

On 3/15/07, Olivier Renaud <o.renaud@laposte.net> 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.

--
Rick DeNatale

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

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

···

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

On 3/15/07, Olivier Renaud <o.renaud@laposte.net> 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?

--
Olivier Renaud