I’d like to access the source of a class like so:
# Module inside file1.rb
module MetaFoo
class << Object
def bar
# here I'd like to access the source location of the Foo class definition
# which should result in /path/to/file2.rb
end
end
end
# Class inside another file2.rb
class Foo
bar
end
I could do something bad like:
self.send(:caller)
and try to parse the output, or even:
class Foo
bar __FILE__
end
But that’s not, want I want, I had the hope there is a more elegant solution for that.
Any hints are welcome.
You could try calling:
caller.first
That will print off the file name and line number. Using your demonstration files above (with slight modifications:
file1.rb:
file2.rb:
When I run
ruby file2.rb, I get the following output:That’s what you want, right?