Ruby:从实例调用类方法
在 Ruby 中,如何从类的实例之一调用该类方法?假设我有
class Truck
def self.default_make
# Class method.
"mac"
end
def initialize
# Instance method.
Truck.default_make # gets the default via the class's method.
# But: I wish to avoid mentioning Truck. Seems I'm repeating myself.
end
end
Truck.default_make
行检索默认值。但是有没有一种方法可以在不提及卡车的情况下表达这一点呢?好像应该有。
In Ruby, how do you call a class method from one of that class's instances? Say I have
class Truck
def self.default_make
# Class method.
"mac"
end
def initialize
# Instance method.
Truck.default_make # gets the default via the class's method.
# But: I wish to avoid mentioning Truck. Seems I'm repeating myself.
end
end
the line Truck.default_make
retrieves the default. But is there a way of saying this without mentioning Truck
? It seems like there should be.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
在实例方法中,您可以直接调用 self.class.whatever,而不是引用类的字面名称。
输出:
Rather than referring to the literal name of the class, inside an instance method you can just call
self.class.whatever
.Outputs:
在继承方面,使用 self.class.blah 与使用 ClassName.blah 不同。
Using
self.class.blah
is NOT the same as usingClassName.blah
when it comes to inheritance.要访问实例方法内的类方法,请执行以下操作:
这是针对您的问题的替代解决方案:
现在让我们使用我们的类:
To access a class method inside a instance method, do the following:
Here is an alternative solution for your problem:
Now let's use our class:
如果您有权访问委托方法,您可以执行以下操作:
或者,如果您想要委托给类和方法的不止一个或两个方法,则可能会更干净。实例:
警告:
不要随机
委托
所有不改变状态的东西给类和实例,因为你会开始遇到奇怪的名称冲突问题。请谨慎执行此操作,并且只有在检查没有其他东西被压扁后才执行此操作。If you have access to the delegate method you can do this:
Alternatively, and probably cleaner if you have more then a method or two you want to delegate to class & instance:
A word of caution:
Don't just randomly
delegate
everything that doesn't change state to class and instance because you'll start running into strange name clash issues. Do this sparingly and only after you checked nothing else is squashed.你正在以正确的方式做这件事。类方法(类似于 C++ 或 Java 中的“静态”方法)不是实例的一部分,因此必须直接引用它们。
在这一点上,在您的示例中,您最好将 'default_make' 设为常规方法:
类方法对于使用该类的实用程序类型函数更有用。例如:
You're doing it the right way. Class methods (similar to 'static' methods in C++ or Java) aren't part of the instance, so they have to be referenced directly.
On that note, in your example you'd be better served making 'default_make' a regular method:
Class methods are more useful for utility-type functions that use the class. For example:
再来一张:
One more:
以下是关于如何实现在这种情况下充当
self.class
的_class
方法的方法。注意:不要在生产代码中使用它,这是为了兴趣:)来自:您可以在 Ruby 中的调用者上下文中评估代码吗? 以及http://rubychallenger.blogspot.com.au/2011/07/caller-binding.html
也许正确的答案是提交 Ruby 补丁:)
Here's an approach on how you might implement a
_class
method that works asself.class
for this situation. Note: Do not use this in production code, this is for interest-sake :)From: Can you eval code in the context of a caller in Ruby? and also http://rubychallenger.blogspot.com.au/2011/07/caller-binding.html
Maybe the right answer is to submit a patch for Ruby :)
与您的问题类似,您可以使用:
Similar your question, you could use: