使用方法创建一个 Method 对象,同时忽略所有类的public_methods



所以我正在制作一颗宝石,我已经对它进行了很好的投入。不幸的是,其中有一个非常重要的错误。此 gem 创建可以附加回调的事件,不幸的是,如果您有与类public_methods之一同名的回调或事件,它就会出错。下面是 gem 错误的工作示例,下面有一些测试代码:

# 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,而是引用该类之外的方法?

下面的代码演示了一个通过初始值设定项将"main"中缺失的方法传递到类中的示例。

class DemoClass
  def initialize method
    @method = method
  end
  def touch *args
    puts 'touch'
    @method.call *args
  end   
end
# Outside the gem
def some_method
  puts 'Unbugged method'
end
def some_private_method
  puts 'Bugged method'
end
non_bugged_instance = DemoClass.new( self.method :some_method )
bugged_instance = DemoClass.new( self.method :some_private_method )
puts "Non bugged touch"
non_bugged_instance.touch
puts "Bugged touch"
bugged_instance.touch

并输出:

Non bugged touch
touch
Unbugged method
Bugged touch
touch
Bugged method

如果强烈建议仅使用方法名称,请将类初始值设定项替换为以下内容:

def initialize method_name
  @method = Kernel.method method_name
end

类创建调用为:

non_bugged_instance = DemoClass.new :some_method
bugged_instance = DemoClass.new :some_private_method

但我真诚地建议使用第一个选项。

好吧,显然,为了引用DemoClass之外的方法,我需要使用 superclass 方法。显然,您还需要引用self.class否则它将尝试调用名为 superclass 的公共方法。总之,它将看起来像这样:

# 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 superclass methods like it's supposed to
    self.class.superclass.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

最新更新