# Question about self and private

**URL:** <https://www.ruby-forum.com/t/question-about-self-and-private/52600>\
**Category:** Rails\
**Created:** [January 17, 2006, 1:24am UTC](https://www.ruby-forum.com/t/question-about-self-and-private/52600 "2006-01-17T01:24:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mischa78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3da27b/32.png) [@mischa78](https://www.ruby-forum.com/u/mischa78)\
**Post date:** [January 17, 2006, 1:24am UTC](https://www.ruby-forum.com/t/question-about-self-and-private/52600/1 "2006-01-17T01:24:41Z")

</div>

Hi!

There’s something I don’t understand yet about static/private methods. I  
hope someone can explain…

E.g.:

class SayHello  
def hello(who)  
puts "Hello, " + who + “!”  
end  
def self.say(words)  
puts words  
end  
end

hello = SayHello.new  
hello.hello “World” : Hello World!  
hello.say “hi” : Error  
SayHello.say “hi” : hi

OK, this is clear to me, but now…

class SayHello  
private  
def hello(who)  
puts "Hello, " + who + “!”  
end  
def self.say(words)  
puts words  
end  
end

Both methods are private now.

hello = SayHello.new  
hello.hello “World” : Error because the method is private  
hello.say “hi” : Error because the method is static  
SayHello.say “hi” : hi

The last one I don’t understand. How come it returns “hi” eventhough  
it’s a private method? Can someone shed some light?

Thanks,  
Mischa.

---

<div class="post-metadata">

**Author:** ![mischa78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3da27b/32.png) [@mischa78](https://www.ruby-forum.com/u/mischa78)\
**Post date:** [January 17, 2006, 11:06am UTC](https://www.ruby-forum.com/t/question-about-self-and-private/52600/2 "2006-01-17T11:06:05Z")

</div>

Mischa B. wrote:

> Both methods are private now.  
> Nope private, protected and public modifiers have only effect on  
> instance  
> methods (that have an implicit “self” as receiver). If you want to  
> define  
> a private class method you need to make available that implicit “self”:

class SayHello  
private  
def hello(who)  
puts "Hello, " + who + “!”  
end  
class \<\< self  
private  
def say(words)  
puts words  
end  
end  
end

Now both methods are private. You can only call them on self, this means  
that SayHello.say is only callable by another class method.

zsombor

---

<div class="post-metadata">

**Author:** ![mischa78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3da27b/32.png) [@mischa78](https://www.ruby-forum.com/u/mischa78)\
**Post date:** [January 17, 2006, 1:00pm UTC](https://www.ruby-forum.com/t/question-about-self-and-private/52600/3 "2006-01-17T13:00:18Z")

</div>

On Tue, 2006-01-17 at 11:29 +0200, Dee Z. wrote:

> end  
> end

You can’t do this though:

class SayHello  
self.private  
def self.say(words)  
puts words  
end  
end

as “private” is itself a private method 🙂

NoMethodError: private method `private’ called for SayHello:Class

---

<div class="post-metadata">

**Author:** ![mischa78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3da27b/32.png) [@mischa78](https://www.ruby-forum.com/u/mischa78)\
**Post date:** [January 17, 2006, 10:11pm UTC](https://www.ruby-forum.com/t/question-about-self-and-private/52600/4 "2006-01-17T22:11:30Z")

</div>

Dee Z. wrote:

> > end
> > 
> > Both methods are private now.
> 
> Nope private, protected and public modifiers have only effect on instance  
> methods (that have an implicit “self” as receiver). If you want to define  
> a private class method you need to make available that implicit “self”:

Well, actually I was asking cause I saw a code sample in the “Agile Web  
Development with Rails” book (pp. 510: private def self.hash\_password)  
that didn’t make sense to me. I don’t mind my methods not being private,  
so I guess I’ll just leave out “private”. Thanks for clearing things up.

Mischa.

---

<div class="post-metadata">

**Author:** ![mischa78](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3da27b/32.png) [@mischa78](https://www.ruby-forum.com/u/mischa78)\
**Post date:** [January 20, 2006, 11:26am UTC](https://www.ruby-forum.com/t/question-about-self-and-private/52600/5 "2006-01-20T11:26:43Z")

</div>

> > > class SayHello  
> > > private  
> > > def hello(who)  
> > > puts "Hello, " + who + “!”  
> > > end  
> > > def self.say(words)  
> > > puts words  
> > > end  
> > > end

An even simpler way is to use ‘private\_class\_method’

class SayHello  
def self.say(words)  
puts words  
end  
private\_class\_method :say  
end

Zsombor
