module MethodIntrospection::SourceLocation::UnboundMethodExtensions

#

UnboundMethodExtensions

#

Public Instance Methods

source_location() click to toggle source
#

source_location

Return the source location of an instance method for Ruby 1.8.

@return [Array] A two element array. First element is the

file, second element is the line in the file where the
method definition is found.
#
# File lib/method_introspection/source_location.rb, line 29
def source_location
  klass =
  case owner
  when Class
    owner
  when Module
    method_owner = owner
    Class.new { include(method_owner) }
  end

  # =================================================================== #
  # Deal with immediate values.
  # =================================================================== #
  case
  when klass == Symbol
    return :a.method(name).source_location
  when klass == Fixnum
    return 0.method(name).source_location
  when klass == TrueClass
    return true.method(name).source_location
  when klass == FalseClass
    return false.method(name).source_location
  when klass == NilClass
    return nil.method(name).source_location
  end
  begin
    Object.instance_method(:method).bind(klass.allocate).call(name).source_location
  rescue TypeError
    # Assume we are dealing with a Singleton Class:
    # 1. Get the instance object
    # 2. Forward the source_location lookup to the instance
    instance ||= ObjectSpace.each_object(owner).first
    Object.instance_method(:method).bind(instance).call(name).source_location
  end
end