这种创建私有类方法是如何工作的:

class Person

  def self.get_name
    persons_name
  end

  class << self

    private

    def persons_name
      "Sam"
    end
  end
end

puts "Hey, " + Person.get_name
puts "Hey, " + Person.persons_name  #=> raises "private method `persons_name' called for Person:Class (NoMethodError)"

但这不是:

class Person

  def self.get_name
    persons_name
  end

  private

  def self.persons_name
    "Sam"
  end
end

puts "Hey, " + Person.get_name
puts "Hey, " + Person.persons_name

当前回答

默认情况下,所有类方法都是公共的。为了使它们私有,你可以使用模块#private_class_method,就像@tjwallace写的那样,或者像你所做的那样定义它们:

class << self

  private

  def method_name
    ...
  end
end

类<< self打开了self的单例类,这样就可以为当前self对象重新定义方法。这是用来定义类/模块(“静态”)方法。只有在这里,定义私有方法才能真正提供私有类方法。

其他回答

我也发现Ruby(或者至少我对Ruby的了解)不够到位 在这个地区。举个例子,下面是我想要的,但很笨拙,

class Frob
    attr_reader :val1, :val2

    Tolerance = 2 * Float::EPSILON

    def initialize(val1, val2)
        @val2 = val1
        @val2 = val2
        ...
    end

    # Stuff that's likely to change and I don't want part
    # of a public API.  Furthermore, the method is operating
    # solely upon 'reference' and 'under_test' and will be flagged as having
    # low cohesion by quality metrics unless made a class method.
    def self.compare(reference, under_test)
        # special floating point comparison
        (reference - under_test).abs <= Tolerance
    end
    private_class_method :compare

    def ==(arg)
        self.class.send(:compare, val1, arg.val1) &&
        self.class.send(:compare, val2, arg.val2) &&
        ...
    end
end

My problems with the code above is that the Ruby syntax requirements and my code quality metrics conspire to made for cumbersome code. To have the code both work as I want and to quiet the metrics, I must make compare() a class method. Since I don't want it to be part of the class' public API, I need it to be private, yet 'private' by itself does not work. Instead I am force to use 'private_class_method' or some such work-around. This, in turn, forces the use of 'self.class.send(:compare...' for each variable I test in '==()'. Now that's a bit unwieldy.

Ruby似乎提供了一个糟糕的解决方案。要解释,先从简单的说起 c++示例,显示了对私有类方法的访问:

#include <iostream>

class C
{
    public:
        void instance_method(void)
        {
            std::cout << "instance method\n";
            class_method();  // !!! LOOK !!! no 'send' required. We can access it
                             // because 'private' allows access within the class
        }
    private:
        void static class_method(void) { std::cout << "class method\n"; }
};

int main()
{
    C c;

    c.instance_method(); // works
    // C::class_method() does not compile - it's properly private
    return 0;
}

运行上述程序

   % ./a.out
   instance method
   class method

现在Ruby似乎没有提供类似的功能。我认为,Ruby的规则是这样的 接收方不能访问私有方法。也就是说,

inst.pvt_method  # FAILS
pvt_method # WORKS only within the class (good)

这对于私有实例方法是可以的,但是对于私有类就会产生问题 方法。

我希望Ruby是这样工作的:

class C
    def instance_method
        STDOUT << "instance method\n"

        # Simple access to the private class method would be nice:
        class_method   # DOES NOT WORK. RUBY WON'T FIND THE METHOD
        C.class_method # DOES NOT WORK. RUBY WON'T ALLOW IT

        # ONLY THIS WORKS. While I am happy such capability exists I think
        # the way 'send' should be used is when the coder knows he/she is
        # doing a no-no.  The semantic load on the coder for this is also
        # remarkably clumsy for an elegant language like ruby.
        self.class.send(:class_method)
    end

    private_class_method def self.class_method() STDOUT << "class method\n"; end
end

但是,遗憾的是,上述方法并不奏效。有人知道更好的办法吗?

当我看到'发送'之前的方法,这是一个明显的迹象,代码违反 API的设计者的意图,但在这种情况下,设计是特别的 要使类的实例方法调用私有类方法。

为了完整起见,我们还可以避免在单独的行中声明private_class_method。我个人不喜欢这种用法,但很高兴知道它的存在。

private_class_method  def self.method_name
 ....
end

如果你在显式对象(在你的例子中是self)上定义一个方法,Private似乎不起作用。您可以使用private_class_method将类方法定义为私有的(或者像您描述的那样)。

class Person
  def self.get_name
    persons_name
  end

  def self.persons_name
    "Sam"
  end

  private_class_method :persons_name
end

puts "Hey, " + Person.get_name
puts "Hey, " + Person.persons_name

或者(在ruby 2.1+中),因为方法定义返回方法名的符号,你也可以这样使用:

class Person
  def self.get_name
    persons_name
  end

  private_class_method def self.persons_name
    "Sam"
  end
end

puts "Hey, " + Person.get_name
puts "Hey, " + Person.persons_name

默认情况下,所有类方法都是公共的。为了使它们私有,你可以使用模块#private_class_method,就像@tjwallace写的那样,或者像你所做的那样定义它们:

class << self

  private

  def method_name
    ...
  end
end

类<< self打开了self的单例类,这样就可以为当前self对象重新定义方法。这是用来定义类/模块(“静态”)方法。只有在这里,定义私有方法才能真正提供私有类方法。