开发者

workaround for ruby 1.9.2: super from singleton method that is defined to multiple classes is not supported

Ruby 1.9.2 disallows calling "super" from singleton methods defined to multiple classes.

Often the best solution is to just stop using singleton methods. However, we're redefining self.inherited, which is part of the language definition, so that's not possible.

Has anybody found a good workaround? I've tried:

def self.inherited(klass)
  # ...
  klass.ancestors.each do |kls|
开发者_如何学编程   if kls.respond_to?(:inherited) && !kls.include?(MyModuleName)
     kls.inherited(klass)
    end
  end
end

Which doesn't work.

I've also tried alias_method_chain'ing the function, and that didn't work either.

More context is available at https://hobo.lighthouseapp.com/projects/8324-hobo/tickets/840 && http://github.com/tablatom/hobo, although you can also just ask for more context if I haven't provided enough.


Domizio Demichelis found the answer, and it's a strange one:

eval %(
  def self.inherited(klass)
    # ...
    klass.ancestors.each do |kls|
     if kls.respond_to?(:inherited) && !kls.include?(MyModuleName)
       kls.inherited(klass)
      end
    end
  end
)

That's right -- wrap the function in an eval. The eval's outside the function, so there's no additional run time cost.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜