How to find source_location of the code executed by super?

inheritance, ruby, super

Solution

From ruby 2.2 you can use `super_method` like this:

Class A
  def pr
    puts "pr"
  end
end

Class B < A
  def pr
    puts "Super method: #{method(:pr).super_method}"
  end
end

As `super_method` returns a Method, you can chain them to find the ancestor:

def ancestor(m)
  m = method(m) if m.is_a? Symbol
  super_m = m.super_method
  if super_m.nil?
    return m
  else
    return ancestor super_m
  end
end

Problem

``` class C1 def pr puts 'C1' end end class C2 < C1 def pr puts 'C2' super puts self.method(:pr).source_location end end c = C2.new c.pr ``` In the program above is it possible to obtain location of the code executed by `super` (`C1::pr` in our case) as well as we obtain the location of `C2::pr` code using `source_location` method?

Original source