Accesing to private attributes

  • Thread starter Imobach González Sosa
  • Start date
I

Imobach González Sosa

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

Dan Doel

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
 
R

Robert Klemme

Dan Doel said:
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 :mad:bar, "ha!"
=> "ha!"
irb(main):011:0> f.instance_variable_get :mad:bar
=> "ha!"
irb(main):012:0>

robert
 
G

Ged

Dan Doel said:
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?
 
D

Dan Doel

Ged said:
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
 

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,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top