# Basic ruby Question

**URL:** <https://www.ruby-forum.com/t/basic-ruby-question/142180>\
**Category:** Ruby\
**Created:** [July 4, 2008, 7:16am UTC](https://www.ruby-forum.com/t/basic-ruby-question/142180 "2008-07-04T07:16:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deepak\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@Deepak\_G](https://www.ruby-forum.com/u/Deepak_G)\
**Post date:** [July 4, 2008, 7:16am UTC](https://www.ruby-forum.com/t/basic-ruby-question/142180/1 "2008-07-04T07:16:32Z")

</div>

Hello,

I didn’t understand the following concept.

\*class Universe

private  
def self.private\_method  
p “=========Hello world=======”  
end

end

Universe. **private\_method** =\> “=========Hello world=======”  
\*

How come I get the o/p ( \*"=========Hello world=======" \* ) as I have  
declared that method as private.

Thanks in advance  
Deepak G. (DG)

---

<div class="post-metadata">

**Author:** ![Deepak\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@Deepak\_G](https://www.ruby-forum.com/u/Deepak_G)\
**Post date:** [July 4, 2008, 7:47am UTC](https://www.ruby-forum.com/t/basic-ruby-question/142180/2 "2008-07-04T07:47:10Z")

</div>

Hi All,

But it was working in this way.

class Universe  
def self.private\_method  
p “=========Hello world=======”  
end  
end  
u=Universe.new  
u.private\_method

# =\> private.rb:7: undefined method `private\_method’ for

#Universe:0x2b66aa0 (NoMethodError)

what is the Difference betweeen these two types?

u=Universe.new  
u.private\_method && Universe.private\_method

Regards,  
P.Raveendran

> **[Raveendran](https://raveendran.wordpress.com)**
>
> Nothing is impossible...

---

<div class="post-metadata">

**Author:** ![Deepak\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@Deepak\_G](https://www.ruby-forum.com/u/Deepak_G)\
**Post date:** [July 4, 2008, 8:03am UTC](https://www.ruby-forum.com/t/basic-ruby-question/142180/3 "2008-07-04T08:03:41Z")

</div>

Raveendran J. wrote:

> But it was working in this way.
> 
> class Universe  
> def self.private\_method  
> p “=========Hello world=======”  
> end  
> end  
> u=Universe.new  
> u.private\_method
> 
> # =\> private.rb:7: undefined method `private\_method’ for
> 
> #Universe:0x2b66aa0 (NoMethodError)

Your second example, does not work either. You tried to call a  
class-method on an object.

I don’t know how useful a private class-method could be. it doesn’t make  
to much sense for me.

i’m pretty sure, all you want is a simple instance-method: leave out the  
“self.” at the methods declaration:

private  
def private\_method  
p “something”  
end

> what is the Difference betweeen these two types?
> 
> u=Universe.new  
> u.private\_method && Universe.private\_method

for the difference between instance and class-methods use google on  
there terms. the first explanation i found, should be sufficient:

> **[Ruby Class Tutorial](http://juixe.com/techknow/index.php/2007/01/22/ruby-class-tutorial/)**
>
> Classes and interfaces are the primary building blocks of a typical Java application. Classes are the blue prints used to create objects in a running system. In contrast to Java, Ruby’s build…

---

<div class="post-metadata">

**Author:** ![Deepak\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@Deepak\_G](https://www.ruby-forum.com/u/Deepak_G)\
**Post date:** [July 4, 2008, 9:12am UTC](https://www.ruby-forum.com/t/basic-ruby-question/142180/4 "2008-07-04T09:12:33Z")

</div>

On Friday 04 July 2008, Deepak G. wrote:

> end
> 
> Universe. **private\_method** =\> “=========Hello world=======”  
> \*
> 
> How come I get the o/p ( \*"=========Hello world=======" \* ) as I have  
> declared that method as private.
> 
> Thanks in advance  
> Deepak G. (DG)

The private method only makes instance methods private, not class  
methods.  
This means the reason you can call Universe.private\_method is that,  
despite  
its name, the method is still public. To make a class method private,  
you can  
use the Module#private\_class\_method method:

class Universe

def self.private\_method  
p “Hello world”  
end

private\_class\_method :private\_method

end

Universe.private\_method

=\> private method `private\_method’ called for Universe:Class  
(NoMethodError)

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![Deepak\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@Deepak\_G](https://www.ruby-forum.com/u/Deepak_G)\
**Post date:** [July 7, 2008, 6:57am UTC](https://www.ruby-forum.com/t/basic-ruby-question/142180/5 "2008-07-07T06:57:00Z")

</div>

Hello stefano,

Thanks for your explanation.

On Fri, Jul 4, 2008 at 12:37 PM, Stefano C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
