# Private class methods- functionality or shortcoming?

**URL:** <https://www.ruby-forum.com/t/private-class-methods-functionality-or-shortcoming/139960>\
**Category:** Ruby\
**Created:** [June 9, 2008, 4:02pm UTC](https://www.ruby-forum.com/t/private-class-methods-functionality-or-shortcoming/139960 "2008-06-09T16:02:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 9, 2008, 4:02pm UTC](https://www.ruby-forum.com/t/private-class-methods-functionality-or-shortcoming/139960/1 "2008-06-09T16:02:24Z")

</div>

I just switched to Ruby and came across it.  
If we have to make a class method private then we should write it as:

class Abc  
&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp; class\<\<self  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; private  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; def fun  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; p “hello”  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; end  
&nbsp;&nbsp;&nbsp; end  
&nbsp;&nbsp;&nbsp;  
end

But if we write as:

class Abc  
&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp; private  
&nbsp;&nbsp;&nbsp; def self.fun  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; p “hello”  
&nbsp;&nbsp;&nbsp; end

end

then the class method is not private but behaves as public.

So, is it a carefully designed functionality? If so, then what is the  
thinking behind it?  
OR any other reasons behind it?  
Somebody told me that ruby encourages to define class methods the first  
way.  
But I think that if you are giving more than one methods to do a thing,  
then all should function the same way.

Thanks and Regards,  
Amit

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 9, 2008, 4:13pm UTC](https://www.ruby-forum.com/t/private-class-methods-functionality-or-shortcoming/139960/2 "2008-06-09T16:13:00Z")

</div>

On Monday 09 June 2008, amit saxena wrote:

> &nbsp;&nbsp;&nbsp; end  
> &nbsp;&nbsp;&nbsp; end
> 
> Thanks and Regards,  
> Amit

The Module#private method only works for instance methods, not for class  
method. To make a class method private, you should use the  
Module#private\_class\_method method. Note that, unlike private,  
private\_class\_method only accepts one argument, so that you want to make  
more  
than one method private, you should call it multiple times.

So, the correct method to make the method private using the second form  
is:

class Abc

def self.fun  
p “hello”  
end  
private\_class\_method :fun

end

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 10, 2008, 10:48am UTC](https://www.ruby-forum.com/t/private-class-methods-functionality-or-shortcoming/139960/3 "2008-06-10T10:48:42Z")

</div>

"\> The Module#private method only works for instance methods,

> not for class  
> method."

My question is, why is it so? What’s the advantage in it?

Amit

— On Mon, 6/9/08, Stefano C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> From: Stefano C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> Subject: Re: private class methods- functionality or shortcoming?  
> To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> Date: Monday, June 9, 2008, 7:39 PM  
> On Monday 09 June 2008, amit saxena wrote:
> 
> > I just switched to Ruby and came across it.  
> > If we have to make a class method private then we  
> > should write it as:

class Abc

```
class<<self
    private
    def fun
        p "hello"
    end
end

```

end

> > But if we write as:

class Abc

```
private
def self.fun
    p "hello"
end

```

end

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 11, 2008, 1:45pm UTC](https://www.ruby-forum.com/t/private-class-methods-functionality-or-shortcoming/139960/4 "2008-06-11T13:45:43Z")

</div>

HELP…!!!
