这种创建私有类方法是如何工作的:
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
如果你在显式对象(在你的例子中是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
如果你在显式对象(在你的例子中是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
ExiRe写道:
ruby的这种行为真是令人沮丧。我的意思是如果你移动
以私室自居。方法,那么它不是私有的。但是,如果
你把它移到类<< self,然后它突然工作了。
这太恶心了。
它可能令人困惑,可能令人沮丧,但绝对不会令人恶心。
一旦你理解了Ruby的对象模型和相应的方法查找流,这就非常有意义了,尤其是考虑到private不是一个访问/可见性修饰符,而是一个方法调用(类作为它的接收者),就像这里讨论的那样……在Ruby中没有所谓的“私有部分”。
要定义私有实例方法,您可以在实例的类上调用private来将随后定义的方法的默认可见性设置为private…因此,通过在类的类上调用private来定义私有类方法是非常有意义的。它的元类。
其他主流的、自称为OO的语言可能会提供不那么令人困惑的语法,但在没有Ruby元编程功能的情况下,你肯定会牺牲掉一个令人困惑且不太一致(不一致?)的对象模型。
我也发现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.