Forum: Ruby freeze string fails to 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.
529add8c81336015e4905c5b59d7f237?d=identicon&s=25 unknown (Guest)
on 2007-03-15 16:35
(Received via mailing list)
>> a='asdf'
=> "asdf"
>> a.freeze
=> "asdf"
>> a='qwer'
=> "qwer"

should'nt that throw and exception ???
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-03-15 16:45
(Received via mailing list)
On 15.03.2007 16:33, jwaala.amplify@gmail.com 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
85ae830f08b5ff5e39a1cbae9e3d0205?d=identicon&s=25 Olivier Renaud (Guest)
on 2007-03-15 16:47
(Received via mailing list)
Le jeudi 15 mars 2007 16:35, jwaala.amplify@gmail.com 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
83bbea94631d5cfcd2577cd52e0efbd4?d=identicon&s=25 Fergal J Byrne (Guest)
on 2007-03-15 16:49
(Received via mailing list)
jwaala.amplify@gmail.com 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 ===
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2007-03-15 23:16
(Received via mailing list)
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?

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/
85ae830f08b5ff5e39a1cbae9e3d0205?d=identicon&s=25 Olivier Renaud (Guest)
on 2007-03-16 00:30
(Received via mailing list)
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?

Not more often than the elsif/elseif confusion :D
This topic is locked and can not be replied to.