Module: MethodSource::SourceLocation::UnboundMethodExtensions

Included in:
UnboundMethod
Defined in:
lib/method_source/source_location.rb

Instance Method Summary collapse

Instance Method Details

#source_locationArray

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

Returns:

  • (Array)

    A two element array. First element is the file, second element is the line in the file where the method definition is found.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/method_source/source_location.rb', line 38

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
    klass.allocate.method(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
    instance.method(name).source_location
  end
end