是否有可能在 Ruby 中获得对对象方法的引用(我想知道这是否可以在没有 procs/lambdas 的情况下完成),例如,考虑以下代码:
class X
def initialize
@map = {}
setup_map
end
private
def setup_map
# @map["a"] = get reference to a method
# @map["b"] = get reference to b method
# @map["c"] = get referebce to c method
end
public
def call(a)
@map["a"](a) if a > 10
@map["b"](a) if a > 20
@map["c"](a) if a > 30
end
def a(arg)
puts "a was called with #{arg}"
end
def b(arg)
puts "b was called with #{arg}"
end
def c(arg)
puts "c was called with #{arg}"
end
end
有可能做这样的事情吗?我想避免 procs/lambdas 因为我希望能够通过子类化来改变 A,B,C 的行为。