An array updatable?

E

EdUarDo

Hi again,

I have this class:

class A
attr_reader :name, :cars
attr_writer :name

def initialize(name)
@name = name
@cars = Array.new()
end
end

a = A.new("xxxx")
a.cars << "vectra" << "megane"
puts a

output:

vectra
megane

Why can I access cars and modify it if I have not declared it how
attr_writer. Must I simply declare it private?
 
B

Brad Wilson

------=_Part_1909_22907169.1122313829877
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

attr_reader means you can get a reference to the cars collection, to which=
=20
then you are adding items. attr_reader doesn't imply that the thing it's=20
returning will be treated as read only. That it means is that the user is=
=20
not allowed to replace the collection itself.

=20
Hi again,
=20
I have this class:
=20
class A
attr_reader :name, :cars
attr_writer :name
=20
def initialize(name)
@name =3D name
@cars =3D Array.new()
end
end
=20
a =3D A.new("xxxx")
a.cars << "vectra" << "megane"
puts a
=20
output:
=20
vectra
megane
=20
Why can I access cars and modify it if I have not declared it how
attr_writer. Must I simply declare it private?
=20
=20


--=20
Brad Wilson
http://www.dotnetdevs.com/
http://www.agileprogrammer.com/dotnetguy/

Peter: "Oh my god, Brian, there's a message in my Alphabits. It says,=20
'Oooooo.' "
Brian: "Peter, those are Cheerios."
- Family Guy

------=_Part_1909_22907169.1122313829877--
 
J

Joel VanderWerf

EdUarDo said:
Hi again,

I have this class:

class A
attr_reader :name, :cars
attr_writer :name

def initialize(name)
@name = name
@cars = Array.new()
end
end

a = A.new("xxxx")
a.cars << "vectra" << "megane"
puts a

output:

vectra
megane

Why can I access cars and modify it if I have not declared it how
attr_writer. Must I simply declare it private?

a.cars = nil # This gives an error

The restriction is on the variable, not on the object that is bound to
the variable. To restrict that object, you can #freeze it, but that will
affect all access, including within the class A.
 
J

James Edward Gray II

Hi again,

I have this class:

class A
attr_reader :name, :cars
attr_writer :name

def initialize(name)
@name = name
@cars = Array.new()
end
end

a = A.new("xxxx")
a.cars << "vectra" << "megane"
puts a

output:

vectra
megane

Why can I access cars and modify it if I have not declared it how
attr_writer. Must I simply declare it private?

Making a writer for it would mean you could set cars with code like:

a.cars = Hash.new

The code you show is not changing cars, it's just calling methods on
the Array cars holds.

If you want to disallow this, you'll need to do some forwarding, for
the methods you wish to allow. However, I say think and make sure
this is strictly necessary before you bolt down the gates. Don't
kill yourself securing what doesn't strictly need securing. If I
type a.cars.clear I either know what I'm doing or deserve to see
things explode. It's my opinion that neither of those are your
problem. ;)

James Edward Gray II
 
J

Joe Cheng

EdUarDo said:
class A
attr_reader :name, :cars
attr_writer :name

def initialize(name)
@name = name
@cars = Array.new()
end
end

<snip>

Why can I access cars and modify it if I have not declared it how
attr_writer. Must I simply declare it private?

How about this?

class A
attr :name

def cars
@cars.dup.freeze
end

def initialize(name)
@name = name
@cars = Array.new()
end
end
 
M

Markus Koenig

Joe Cheng schrieb:
Joe said:
How about this?

class A
attr :name

def cars
@cars.dup.freeze
end

def initialize(name)
@name = name
@cars = Array.new()
end
end

foo = A.new
# ...
foo.cars.first.replace 'xyzzy'

;)
 

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

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top