# Access specifiers

**URL:** <https://www.ruby-forum.com/t/access-specifiers/69485>\
**Category:** Ruby\
**Created:** [September 21, 2006, 10:49am UTC](https://www.ruby-forum.com/t/access-specifiers/69485 "2006-09-21T10:49:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sourav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a4c791/32.png) [@sourav](https://www.ruby-forum.com/u/sourav)\
**Post date:** [September 21, 2006, 10:49am UTC](https://www.ruby-forum.com/t/access-specifiers/69485/1 "2006-09-21T10:49:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sourav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a4c791/32.png) [@sourav](https://www.ruby-forum.com/u/sourav)\
**Post date:** [September 21, 2006, 12:41pm UTC](https://www.ruby-forum.com/t/access-specifiers/69485/2 "2006-09-21T12:41:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sourav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a4c791/32.png) [@sourav](https://www.ruby-forum.com/u/sourav)\
**Post date:** [September 21, 2006, 1:39pm UTC](https://www.ruby-forum.com/t/access-specifiers/69485/3 "2006-09-21T13:39:28Z")

</div>

On 9/21/06, Sourav [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

Sorry for being blunt, as a matter of fact I am not, it is just the  
timing  
of the response ☹  
The sooner you forget about C++ the sooner you will get Ruby.

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?

This is a very reasonable point, it is all about “enabeling” and  
“controlling” approach.

Ruby happens to be very liberal and of course there are downs. (so  
comparing  
with C++ will give you lots of headaches I am afraifd)  
For most of the part we - those who love ruby - think that the ups  
outweight the downs.  
The example you have given is not really anything unusual for the ruby  
philosophy.  
Look at this e.g

a = Object.new  
class \<\< a  
private  
def hello  
puts “Hi there”  
end  
end  
a.send :hello

I’m a beginner, so please let me know if I’m missing anything. Thanks

> in advance.

Welcome to the big adventure ?

## Robert

Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein

---

<div class="post-metadata">

**Author:** ![sourav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a4c791/32.png) [@sourav](https://www.ruby-forum.com/u/sourav)\
**Post date:** [September 21, 2006, 4:16pm UTC](https://www.ruby-forum.com/t/access-specifiers/69485/4 "2006-09-21T16:16:02Z")

</div>

Sourav wrote:

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

---

<div class="post-metadata">

**Author:** ![sourav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a4c791/32.png) [@sourav](https://www.ruby-forum.com/u/sourav)\
**Post date:** [September 21, 2006, 1:47pm UTC](https://www.ruby-forum.com/t/access-specifiers/69485/5 "2006-09-21T13:47:30Z")

</div>

Even classes like String,Fixnum… are open  
its not the same with other language like java…

ruby shines or sucks  
its up to you how you use features of ruby

just have fun

---

<div class="post-metadata">

**Author:** ![sourav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a4c791/32.png) [@sourav](https://www.ruby-forum.com/u/sourav)\
**Post date:** [September 21, 2006, 4:36pm UTC](https://www.ruby-forum.com/t/access-specifiers/69485/6 "2006-09-21T16:36:14Z")

</div>

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