Accesing to private attributes

Hi all,

I was discusing about Object Oriented Languages with a friend and I have a
question to do. Well, using Python, you can access to a private member of a
class just prepending an underscore and the class name (i.e.:
_myClass__myAttribute). It doesn’t sound good to me. I don’t like private
attributes being accesible from outside.

So, I’d like to know if you can do it on Ruby. I think no, that you must to
use a method, but I’m in doubt.

Thank you.

···


Imobach González Sosa
imodev@softhome.net
osoh@jabber.org

Ruby doesn’t really get in your way if you need to do something like this.
It does, however, give you alarming looking keywords and syntax, so yo at
least see the code is smelly:

class AA; def initialize; @a = 1; end; end

aa = AA.new
aa.instance_eval { puts @a; @a = 2 }
aa.instance_variable_set :@a, 17
aa.instance_variable_get :@a

David
http://homepages.ihug.com.au/~naseby/

···

-----Original Message-----
From: Imobach González Sosa [mailto:imodev@softhome.net]
I was discusing about Object Oriented Languages with a friend and
I have a
question to do. Well, using Python, you can access to a private
member of a
class just prepending an underscore and the class name (i.e.:
_myClass__myAttribute). It doesn’t sound good to me. I don’t like private
attributes being accesible from outside.

So, I’d like to know if you can do it on Ruby. I think no, that
you must to
use a method, but I’m in doubt.

You can do it.

[dolio 05:55:13 ~]$ cat prac.rb
#!/usr/bin/ruby

class Foo
attr_reader :bar
end

obj = Foo.new

obj.instance_eval { @bar = 3 }

p obj.bar
[dolio 05:55:17 ~]$ ./prac.rb
3
[dolio 05:55:23 ~]$

However, I think that, generally, this isn’t a bad thing. In both Ruby and
Python, it’s obvious what you’re doing. You’re not likely to type that in
by accident. Hiding instance variables may be good programming practice,
but that doesn’t mean you shouldn’t be able to get at them if it’s
absolutely
necessary.

You shouldn’t use that kind of thing a lot, but you have to trust people not
to be stupid.

  • Dan

“Dan Doel” djd15@po.cwru.edu schrieb im Newsbeitrag
news:401AE1AC.6090201@po.cwru.edu…

You can do it.

[dolio 05:55:13 ~]$ cat prac.rb
#!/usr/bin/ruby

class Foo
attr_reader :bar
end

obj = Foo.new

obj.instance_eval { @bar = 3 }

p obj.bar
[dolio 05:55:17 ~]$ ./prac.rb
3
[dolio 05:55:23 ~]$

or:

irb(main):009:0> f=Foo.new
=> #Foo:0x100dd198
irb(main):010:0> f.instance_variable_set :@bar, “ha!”
=> “ha!”
irb(main):011:0> f.instance_variable_get :@bar
=> “ha!”
irb(main):012:0>

robert

Dan Doel djd15@po.cwru.edu wrote in message news:401AE1AC.6090201@po.cwru.edu

You can do it.

[dolio 05:55:13 ~]$ cat prac.rb
#!/usr/bin/ruby

class Foo
attr_reader :bar
end

obj = Foo.new

obj.instance_eval { @bar = 3 }

p obj.bar
[dolio 05:55:17 ~]$ ./prac.rb
3
[dolio 05:55:23 ~]$

However, I think that, generally, this isn’t a bad thing. In both Ruby and
Python, it’s obvious what you’re doing. You’re not likely to type that in
by accident. Hiding instance variables may be good programming practice,
but that doesn’t mean you shouldn’t be able to get at them if it’s
absolutely
necessary.

You shouldn’t use that kind of thing a lot, but you have to trust people not
to be stupid.

  • Dan

Dan,

Is there a safety level ( http://www.rubycentral.com/book/taint.html )
that can prevent this methods from being used?

Ged wrote:

Is there a safety level ( http://www.rubycentral.com/book/taint.html )
that can prevent this methods from being used?

As far as I can tell, no.

You could just undefine those methods. You can do that on a per-object
basis:

o = Object.new

class << o
undef :instance_eval
undef :instance_variable_set
undef :instance_variable_get
# etc
end

o.instance_eval { @a = 1 } #=> error