Project

General

Profile

Actions

Feature #9781

closed

Feature Proposal: Method#super_method

Added by schneems (Richard Schneeman) over 10 years ago. Updated almost 10 years ago.

Status:
Closed
Target version:
[ruby-core:62202]

Description

When super is called in a method the Ruby VM knows how to find the next ancestor that has that method and call it. It is difficult to do this manually, so I propose we expose this information in Method#super_location.

Ruby Method class (http://www.ruby-doc.org/core-2.1.1/Method.html) is returned by calling Object.method and passing in a method name (http://www.ruby-doc.org/core-2.1.1/Object.html#method-i-method). This is useful for debugging:

# /tmp/code.rb
class Foo
  def bar
  end
end

puts Foo.new.method(:bar).source_location
# => ["/tmp/code.rb", 3]

The Object#method allows a ruby developer to easily track the source location of the method and makes debugging very easy. However if the code is being invoked by a call to super it is difficult to track down:

# /tmp/code.rb

class BigFoo
  def bar
  end
end

class Foo < BigFoo
  def bar
    super
  end
end

In this code sample it is easy to find the method definition inside of Foo but it is very difficult in large projects to find what code exactly super is calling. This simple example is easy, but it can be hard when there are many ancestors. Currently if I wanted to find this we can inspect ancestors

Foo.ancestors[1..-1].map do |ancestor|
  next unless ancestor.method_defined?(:bar)
  ancestor.instance_method(:bar)
end.compact.first.source_location

To make this process simpler I am proposing a method on the Method class that would return the result of super

It could be called like this:

Foo.new.method(:bar).super_method

I believe adding Method#super_method, or exposing this same information somewhere else, could greatly help developers to debug large systems easily.


Related issues 2 (0 open2 closed)

Related to Ruby master - Feature #7836: Need a way to get Method and UnboundMethod objects to methods overridden by prepended modulesClosedmatz (Yukihiro Matsumoto)Actions
Has duplicate Ruby master - Feature #10216: Add methods to Method and UnboundMethod classess to retrieve method instance for superClosedmatz (Yukihiro Matsumoto)Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0