私有类方法

要让你的类方法变得私有,你需要使用 private_class_method :method_name 或者把你的私有类方法放到class << self block 中:

class Foo

  class << self
    private    
    def bar
      puts 'Class method called'
    end    
  end

  def self.baz
    puts 'Another class method called'
  end
  private_class_method :baz

end

Foo.bar # => NoMethodError: private method `bar' called for Foo:Class
Foo.baz # => NoMethodError: private method `baz' called for Foo:Class
posted on 2014-05-07 14:12  qinyan20  阅读(190)  评论(0编辑  收藏  举报