[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

access specifiers

Sourav

9/21/2006 8:45:00 AM

Hi,
I am new to Ruby and after progamming in several other languages, I am
really enjoying the fun of Ruby. I had question regarding the access
specifiers in Ruby; suppose the following two classes,

class C1
private
def aMethod
"I am a method"
end
end

class C2 < C1
public :aMethod
end

In C1, aMethod was private, but in C2 it became public... so this way,
any private method of a class can be converted to public methods. So
then what is the use of having them (private methods) in the first
place at all! In C++, there is a rule that, an object (data or
function) of lower access-specifier can be upgraded to higher
acc-specifier but the reverse is not true. Should it not also be
implemented in Ruby?

I'm a beginner, so please let me know if I'm missing anything. Thanks
in advance.

3 Answers

MonkeeSage

9/21/2006 10:35:00 AM

0

Sourav wrote:
> In C1, aMethod was private, but in C2 it became public... so this way,
> any private method of a class can be converted to public methods. So
> then what is the use of having them (private methods) in the first
> place at all! In C++, there is a rule that, an object (data or
> function) of lower access-specifier can be upgraded to higher
> acc-specifier but the reverse is not true. Should it not also be
> implemented in Ruby?

Hi there,

In ruby they are more like hints. If the programmer wants to make a
method public, then the assumption is that they know what they are
doing.

You could make it harder on them (and yourself) to make something
public again by overriding the Class#public method:

class Class
def public(*methods)
warn "You naughty programmer, that's private!"
end
end

But I wouldn't recommend doing it.

Regards,
Jordan

Gene Tani

9/21/2006 2:13:00 PM

0


Sourav wrote:
> Hi,
> I am new to Ruby and after progamming in several other languages, I am
> really enjoying the fun of Ruby. I had question regarding the access
> specifiers in Ruby; suppose the following two classes,
>
> class C1
> private
> def aMethod
> "I am a method"
> end
> end
>
> class C2 < C1
> public :aMethod
> end
>
> In C1, aMethod was private, but in C2 it became public... so this way,
> any private method of a class can be converted to public methods. So
> then what is the use of having them (private methods) in the first
> place at all! In C++, there is a rule that, an object (data or
> function) of lower access-specifier can be upgraded to higher
> acc-specifier but the reverse is not true. Should it not also be
> implemented in Ruby?
>

Yes, there's a few ways to circumvent "private" declarations:
Object#send (this will change in 1.9), delegate with instance_eval,
anonymous modules. In your example, you don't have to subclass the
class with private methods, you can simply re-open the class definition
and declare them public.

Sourav

9/21/2006 2:32:00 PM

0


Hmmmm... thnx, I got it. In Ruby it's not that much rigid as C++/Java.
And I think it's better like this.