# Private visibility for class method

**URL:** <https://www.ruby-forum.com/t/private-visibility-for-class-method/152333>\
**Category:** Ruby\
**Created:** [November 2, 2008, 3:50pm UTC](https://www.ruby-forum.com/t/private-visibility-for-class-method/152333 "2008-11-02T15:50:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lianliming](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b38774/32.png) [@lianliming](https://www.ruby-forum.com/u/lianliming)\
**Post date:** [November 2, 2008, 3:50pm UTC](https://www.ruby-forum.com/t/private-visibility-for-class-method/152333/1 "2008-11-02T15:50:53Z")

</div>

I got to learn that “private” label in a class definition doesn’t  
apply to class methods. But I saw many existed source code defining  
class methods after “private” label. For example, in the book “Agile  
Web D. with Rails 2nd”, I saw following code snippet:

class User \< ActiveRecord::Base  
…

def password=(pwd)  
…  
end

private

def self.encrypted\_password(password, salt)  
…  
end

end

If private has no effect on the visibility of class method  
“self.encrypted\_password”, I am wondering why the author put its  
definition there. This isn’t the only place I found class methods are  
defined under “private” label. So I guess there may be other  
intentions I don’t know. Is there anyone giving some hints on this?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![lianliming](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b38774/32.png) [@lianliming](https://www.ruby-forum.com/u/lianliming)\
**Post date:** [November 3, 2008, 8:35am UTC](https://www.ruby-forum.com/t/private-visibility-for-class-method/152333/2 "2008-11-03T08:35:26Z")

</div>

> If private has no effect on the visibility of class method  
> “self.encrypted\_password”, I am wondering why the author put its  
> definition there. This isn’t the only place I found class methods are  
> defined under “private” label. So I guess there may be other  
> intentions I don’t know. Is there anyone giving some hints on this?

I guess that author didn’t know that it won’t have any affect.  
Intuitively it should work as author expected but it doesn’t (and  
there are some reason). If you want to make private/protected class  
methods do this:

class User \< ActiveRecord::Base  
class \<\< self  
private

```
def encrypted_password(password, salt)
   ....
end

```

end  
end

–  
Rados³aw Bu³at

[http://radarek.jogger.pl](http://radarek.jogger.pl) - mój blog
