Ruby:如何通过对象引用调用函数?
考虑这个人为的示例:
# Dispatch on value of fruit_kind:
TYPE_A = :apple
TYPE_B = :banana
TYPE_C = :cherry
eating_method = nil
case fruit_kind
# Methods to use for different kinds of fruit (assume these are
# already defined)
when TYPE_A then eating_method = bite
when TYPE_B then eating_method = peel
when TYPE_C then eating_method = om_nom_nom
end
现在我想使用一些参数调用 eating_method
的目标:
# Doesn't work; this tries to invoke a method called "eating_method",
# not the reference I defined earlier.
eating_method(some_fruit)
在 Ruby 中执行此操作的正确方法是什么?
Consider this contrived example:
# Dispatch on value of fruit_kind:
TYPE_A = :apple
TYPE_B = :banana
TYPE_C = :cherry
eating_method = nil
case fruit_kind
# Methods to use for different kinds of fruit (assume these are
# already defined)
when TYPE_A then eating_method = bite
when TYPE_B then eating_method = peel
when TYPE_C then eating_method = om_nom_nom
end
Now I'd like to invoke the target of eating_method
with some arguments:
# Doesn't work; this tries to invoke a method called "eating_method",
# not the reference I defined earlier.
eating_method(some_fruit)
What's the right way to do this in Ruby?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
发送
。 Send 接受函数名称,因此使用符号:编辑:
顺便说一句,您知道可以通过执行以下操作使
case
变得更漂亮吗:或者,正如 Sii 提到的,使用散列代替:
Use
send
. Send takes the function name, so use symbols:EDIT:
By the way, did you know you can make that
case
a little prettier by doing something like this:Or, as Sii mentions, use a hash instead:
在 Ruby 中,您可以像使用值一样使用类和方法,而无需太多工作,因此您可以将示例中实际需要管理的数量减少到 Class -> 的单个定义。 方法并使用通用算法来处理该定义。
假设每个类型都实现了您指定的方法,例如 Apple.bite()、Banana.peel() 和 Cherry.om_nom_nom() 都已定义。 还假设 fruit 是 fruit_kind 的实例,您可以在一个位置管理映射,并使用通用方法来执行所有类特定方法评估,如下所示:
请注意引用eating_method是特定于水果实例的方法对象的实例。 您可以将表定义拉出为类变量,但您需要在每次决定对传递的实例调用哪个函数时的上下文中评估fruit.method。
In Ruby you can use classes and methods as if they are values without too much work, thus you can reduce the amount you actually have to manage in your example to a single definition of Class -> Method and use a generalized algorithm to work with that definition.
Suppose each of your types implement the methods you specify, e.g. Apple.bite(), Banana.peel(), and Cherry.om_nom_nom() are all defined. Also suppose that fruit is an instance of fruit_kind You can manage the mapping in one place and use a generalized method to do all of the Class specific method evaluation like so:
Note that the reference eating_method is an instance of a method object specific to the instance of fruit. You could pull the table definition out to be a class variable, but you would want to evaluate fruit.method in the context of each time that you are deciding which function to call on an instance you are passed.
措辞让我非常困惑。
不过,您可能需要 Object#send 。
The wording confuses me greatly.
You probably want Object#send though.
Ruby 对象模型允许您使用
Object#send
方法动态调用方法,该方法将符号作为您要调用的方法。因此,如果您有一个名为 FruitEater 的类,您可以将 eat 方法发送为:
The Ruby object model lets you call methods dynamically using the
Object#send
method which takes a symbol as the method you want to call.So if you were to have a class called FruitEater you could send the eating method as: