File: super_method.rb

package info (click to toggle)
ruby-backports 3.25.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,912 kB
  • sloc: ruby: 11,759; makefile: 6
file content (16 lines) | stat: -rw-r--r-- 590 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
unless Method.method_defined? :super_method
  require 'backports/1.8.7/array/find_index'

  class Method
    def super_method
      singleton_klass = class << receiver; self; end
      call_chain = singleton_klass.ancestors
      # find current position in call chain:
      skip = call_chain.find_index {|c| c == owner} or return
      call_chain = call_chain.drop(skip + 1)
      # find next in chain with a definition:
      next_index = call_chain.find_index {|c| c.method_defined? name}
      next_index && call_chain[next_index].instance_method(name).bind(receiver)
    end
  end
end