[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

appending an array to a frozen array

James French

3/25/2009 10:30:00 AM

Morning all,

@dependencies.freeze
@dependencies +=3D ["blah", "blah"]

does not error out due to modifying a frozen array (1.8.7 p72).

Is this expected behaviour? Up until now I've considered freezing an array =
as making the array completely read only.

Cheers,
James

4 Answers

lasitha

3/25/2009 10:48:00 AM

0

On Wed, Mar 25, 2009 at 4:00 PM, James French
<James.French@naturalmotion.com> wrote:
> Morning all,
>
> @dependencies.freeze
> @dependencies += ["blah", "blah"]
>
> does not error out due to modifying a frozen array (1.8.7 p72).
> [...]

+= creates a new array and assigns it to @dependencies. The original
array remains frozen but @dependencies no longer refers to it.

To illustrate:
$: irb
01> a = b = [1, 2]
--> [1, 2]
02> a.freeze
--> [1, 2]
03> b.frozen?
--> true
04> b += [3, 4]
--> [1, 2, 3, 4]
05> a.frozen?
--> true

solidarity,
lasitha

James French

3/25/2009 11:03:00 AM

0

Ah, I understand - thanks. My next question is - is it possible to prevent =
this?

________________________________________
From: lasitha [lasitha.ranatunga@gmail.com]
Sent: 25 March 2009 10:48
To: ruby-talk ML
Subject: Re: appending an array to a frozen array

On Wed, Mar 25, 2009 at 4:00 PM, James French
<James.French@naturalmotion.com> wrote:
> Morning all,
>
> @dependencies.freeze
> @dependencies +=3D ["blah", "blah"]
>
> does not error out due to modifying a frozen array (1.8.7 p72).
> [...]

+=3D creates a new array and assigns it to @dependencies. The original
array remains frozen but @dependencies no longer refers to it.

To illustrate:
$: irb
01> a =3D b =3D [1, 2]
--> [1, 2]
02> a.freeze
--> [1, 2]
03> b.frozen?
--> true
04> b +=3D [3, 4]
--> [1, 2, 3, 4]
05> a.frozen?
--> true

solidarity,
lasitha=

Joel VanderWerf

3/25/2009 4:49:00 PM

0

James French wrote:
> Ah, I understand - thanks. My next question is - is it possible to prevent this?
...
>> @dependencies.freeze
>> @dependencies += ["blah", "blah"]
>>
>> does not error out due to modifying a frozen array (1.8.7 p72).

Use an attr_reader rather than an instance variable?

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Robert Klemme

3/25/2009 4:50:00 PM

0

2009/3/25 James French <James.French@naturalmotion.com>:
> Ah, I understand - thanks. My next question is - is it possible to prevent this?

Since you are using an instance variable you control how it is
accessed. So, yes. If you want to know whether there is a hard way to
prevent reassignment to instance variables, the only safe way I know
off the top of my head is to freeze the owner:

irb(main):001:0> o=Object.new
=> #<Object:0x10171ed8>
irb(main):002:0> o.instance_variable_set "@foo", 1
=> 1
irb(main):003:0> o
=> #<Object:0x10171ed8 @foo=1>
irb(main):004:0> o.freeze
=> #<Object:0x10171ed8 @foo=1>
irb(main):005:0> o.instance_variable_set "@foo", 2
RuntimeError: can't modify frozen object
from (irb):5:in `instance_variable_set'
from (irb):5
from /opt/bin/irb19:12:in `<main>'
irb(main):006:0>

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end