Private visibility for class method


#1

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!


#2

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 - mój blog