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