class Introspect::Which

Public Class Methods

new(object) click to toggle source
# File lib/introspect/which.rb, line 7
def initialize object
  @object = object
end
which(object, method_name) click to toggle source
# File lib/introspect/which.rb, line 3
def self.which object, method_name
  self.new(object).which method_name
end

Public Instance Methods

which(method_name) click to toggle source
# File lib/introspect/which.rb, line 11
def which method_name
  ometh = proc { |obj,sym| return obj if obj.methods(false).include? sym }
  imeth = proc { |obj,sym| return obj if obj.instance_methods(false).include? sym }

  begin
    # first check the eigenclass
    imeth[(class << @object; self; end),method_name]
  rescue TypeError => err
    raise unless err.message.include? 'singleton'
  end

  obj = @object.is_a?(::Module) ? @object : @object.class

  # then we check the normal class heirarchy
  obj.ancestors.each.with_object method_name, &imeth
  # check the metaclasses
  obj.class.ancestors.each.with_object method_name, &ometh
  obj.class.ancestors.each.with_object method_name, &imeth

  nil
end