appending an array to a frozen array

J

James French

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
 
L

lasitha

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
 
J

James French

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

________________________________________
From: lasitha [[email protected]]
Sent: 25 March 2009 10:48
To: ruby-talk ML
Subject: Re: appending an array to a frozen array

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

Joel VanderWerf

James said:
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?
 
R

Robert Klemme

2009/3/25 James French said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top