[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

freeze string fails to freeze ???

jwaala.amplify

3/15/2007 3:34:00 PM

>> a='asdf'
=> "asdf"
>> a.freeze
=> "asdf"
>> a='qwer'
=> "qwer"

should'nt that throw and exception ???

5 Answers

Robert Klemme

3/15/2007 3:42:00 PM

0

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

Olivier

3/15/2007 3:47:00 PM

0

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

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

--
Olivier Renaud

Fergal J Byrne

3/15/2007 3:48:00 PM

0

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... t:+353 1 855 8951 aim/skype:FergByrne

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



Rick DeNatale

3/15/2007 10:16:00 PM

0

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.denh...

Olivier

3/15/2007 11:29:00 PM

0

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

--
Olivier Renaud