I have a class with a number of static methods. Each one has to call a common method, but I'm trying not to expose this latter method. Making it private would only allow access from an own instance of the class? Protected does not seem like it would solve the problem here either.
我有一个包含许多静态方法的类。每个人都必须调用一个常用的方法,但我试图不暴露后一种方法。将其设为私有只允许从类的自己的实例访问?受保护似乎不会解决这里的问题。
How do I hide do_calc from being called externally in a static context? (Leaving it available to be called from the first two static methods.)
如何隐藏do_calc在静态上下文中被外部调用? (让它可以从前两个静态方法调用。)
class Foo
def self.bar
do_calc()
end
def self.baz
do_calc()
end
def self.do_calc
end
end
3 个解决方案
#1
36
First off, static
is not really part of the Ruby jargon.
首先,静态不是Ruby行话的一部分。
Let's take a simple example:
我们举一个简单的例子:
class Bar
def self.foo
end
end
It defines the method foo
on an explicit object, self
, which in that scope returns the containing class Bar
. Yes, it can be defined a class method, but static does not really make sense in Ruby.
它在显式对象self上定义方法foo,在该范围内返回包含类Bar。是的,它可以定义为一个类方法,但静态在Ruby中没有意义。
Then private
would not work, because defining a method on an explicit object (e.g. def self.foo
) bypasses the access qualifiers and makes the method public.
然后private不起作用,因为在显式对象上定义一个方法(例如def self.foo)会绕过访问限定符并使该方法公开。
What you can do, is to use the class << self
syntax to open the metaclass of the containing class, and define the methods there as instance methods:
你可以做的是使用类<< self语法来打开包含类的元类,并将那里的方法定义为实例方法:
class Foo
class << self
def bar
do_calc
end
def baz
do_calc
end
private
def do_calc
puts "calculating..."
end
end
end
This will give you what you need:
这将为您提供所需:
Foo.bar
calculating...
Foo.baz
calculating...
Foo.do_calc
NoMethodError: private method `do_calc' called for Foo:Class
#2
12
You can define a private class method with private_class_method
like this:
您可以使用private_class_method定义私有类方法,如下所示:
class Foo
def self.bar
do_calc
end
def self.baz
do_calc
end
def self.do_calc
#...
end
private_class_method :do_calc
end
#3
1
Or as of Ruby 2.1:
或者从Ruby 2.1开始:
class Foo
def self.bar
do_calc
end
private_class_method def self.do_calc
#...
end
end
#1
36
First off, static
is not really part of the Ruby jargon.
首先,静态不是Ruby行话的一部分。
Let's take a simple example:
我们举一个简单的例子:
class Bar
def self.foo
end
end
It defines the method foo
on an explicit object, self
, which in that scope returns the containing class Bar
. Yes, it can be defined a class method, but static does not really make sense in Ruby.
它在显式对象self上定义方法foo,在该范围内返回包含类Bar。是的,它可以定义为一个类方法,但静态在Ruby中没有意义。
Then private
would not work, because defining a method on an explicit object (e.g. def self.foo
) bypasses the access qualifiers and makes the method public.
然后private不起作用,因为在显式对象上定义一个方法(例如def self.foo)会绕过访问限定符并使该方法公开。
What you can do, is to use the class << self
syntax to open the metaclass of the containing class, and define the methods there as instance methods:
你可以做的是使用类<< self语法来打开包含类的元类,并将那里的方法定义为实例方法:
class Foo
class << self
def bar
do_calc
end
def baz
do_calc
end
private
def do_calc
puts "calculating..."
end
end
end
This will give you what you need:
这将为您提供所需:
Foo.bar
calculating...
Foo.baz
calculating...
Foo.do_calc
NoMethodError: private method `do_calc' called for Foo:Class
#2
12
You can define a private class method with private_class_method
like this:
您可以使用private_class_method定义私有类方法,如下所示:
class Foo
def self.bar
do_calc
end
def self.baz
do_calc
end
def self.do_calc
#...
end
private_class_method :do_calc
end
#3
1
Or as of Ruby 2.1:
或者从Ruby 2.1开始:
class Foo
def self.bar
do_calc
end
private_class_method def self.do_calc
#...
end
end