【发布时间】:2009-04-30 13:49:21
【问题描述】:
考虑这个人为的例子:
# 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 中执行此操作的正确方法是什么?
【问题讨论】:
标签: ruby idioms method-dispatch