【发布时间】:2012-09-12 17:35:14
【问题描述】:
我想像这样访问一个类的源代码:
# 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
我可以做一些坏事,比如:
self.send(:caller)
并尝试解析输出,甚至:
class Foo
bar __FILE__
end
但这不是我想要的,我希望有一个更优雅的解决方案。
欢迎任何提示。
【问题讨论】:
-
你可以访问方法源的位置,但是要求一个类的位置太模糊了。
标签: ruby-on-rails ruby metaprogramming