Bryan Larsen
Bryan Larsen

Reputation: 10026

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.

Upvotes: 1

Views: 572

Answers (1)

Bryan Larsen
Bryan Larsen

Reputation: 10026

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.

Upvotes: 2

Related Questions