所以我正在制作一颗宝石,并且我已经对此有很大的投入。不幸的是,它有一个非常重要的错误。这个 gem 创建事件,你可以将回调附加到,不幸的是,如果你有一个回调或一个与类的 public_methods 同名的事件,它就会出错。这是 gem 的 bug 的一个工作示例,它下面有一些测试代码:
# Portion of gem that causes bug
class DemoClass
def initialize method_symbol
@method = to_method(method_symbol)
end
def call(*args)
@method.call(*args)
end
def some_private_method
puts 'the private method was called (still bugged)'
end
private
def to_method(method_symbol)
# this right here references public methods when I don't want it to
method(method_symbol)
end
end
# Outside the gem
def some_method
puts 'this is an original method being called'
end
def some_private_method
puts 'the private method was NOT called. Bug fixed!'
end
non_bugged_instance = DemoClass.new(:some_method)
bugged_instance = DemoClass.new(:some_private_method)
non_bugged_instance.call
bugged_instance.call
有没有办法让私有方法使用不引用公共方法to_method
的符号创建方法对象,而是使用该类之外的方法?:add
add