Search code examples
rubyclassprepend

How to call original method after it got prepend from outside


I'm refactoring some old code to fit to Ruby > 2. And there is some alias_method_chain which I want to remove.

But there is some part where the without method get called. Inside the method I have to super for sure but from outside?

Following is given:

module ExtraLog
  def log
    puts "ExtraLog"
    super
  end
end

class Klass
  prepend ExtraLog
  def log
    puts "Log"
  end
end

a = Klass.new
a.log 
#=> "ExtraLog"
#=> "Log"

How can i call the original log method now without that the prepended method get called? Something like a.original_log?


Solution

  • Method#super_method:

    a.method(:log).super_method.call
    #=> Log