Forum: Ruby Private visibility for class method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
lianliming (Guest)
on 2008-11-02 16:50
(Received via mailing list)
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!
Radosław B. (Guest)
on 2008-11-03 09:35
(Received via mailing list)
> 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
This topic is locked and can not be replied to.