Source location of a class

ruby

Solution

[not actually an answer to your problem, but too long for a comment]

`source_location` doesn't seem to help here, because dynamic methods can be created with an arbitrary location:

# my_class.rb

class MyClass
  attr_accessor :foo

  class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
    def bar
    end
  RUBY

  class_eval(<<-RUBY, 'dummy.rb', 42)
    def baz
    end
  RUBY
end

p MyClass.instance_method(:foo).source_location
p MyClass.instance_method(:bar).source_location
p MyClass.instance_method(:baz).source_location

Output:

$ ruby my_class.rb
["my_class.rb", 4]
["my_class.rb", 7]
["dummy.rb", 42]

Problem

I want to find the instance methods defined inside a class (explicitly with `def`, not those deriving from other calls like `attr_accessor`) To do that, I thought of looping the `instance_methods(false)` result and check if each method's `source_location` is the same as the class's source location. How can I find a class's source location?

Original source