如何在运行时访问Ruby类的源文件位置?

时间:2022-01-15 05:35:46

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.

任何提示都是受欢迎的。

2 个解决方案

#1


1  

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:

module MetaFoo
  class << Object
    def bar
      puts caller.first # <== the magic...
    end
  end
end

file2.rb:

require './file1.rb'

class Foo
  bar
end

When I run ruby file2.rb, I get the following output:

当我运行ruby file2.rb时,我得到以下输出:

nat$ ruby file2.rb 
file2.rb:4:in `<class:Foo>'

That's what you want, right?

这就是你想要的,对吧?

#2


2  

Both $0 and __FILE__ will be useful to you.

$ 0和__FILE__都对你有用。

$0 is the path of the running application.

$ 0是正在运行的应用程序的路径。

__FILE__ is the path of the current script.

__FILE__是当前脚本的路径。

So, __FILE__ will be the script or module, even if it's been required.

所以,__ FILE__将是脚本或模块,即使它是必需的。

Also, __LINE__ might be useful to you.

此外,__LINE__可能对您有用。

See "What does __FILE__ mean in Ruby?", "What does if __FILE__ == $0 mean in Ruby" and "What does class_eval <<-“end_eval”, __FILE__, __LINE__ mean in Ruby? for more information.

请参阅“Ruby中__FILE__的含义是什么?”,“如果__FILE__ = = 0 0在Ruby中意味着什么”和“什么使用class_eval << - ”end_eval“,__ FILE __,_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

#1


1  

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:

module MetaFoo
  class << Object
    def bar
      puts caller.first # <== the magic...
    end
  end
end

file2.rb:

require './file1.rb'

class Foo
  bar
end

When I run ruby file2.rb, I get the following output:

当我运行ruby file2.rb时,我得到以下输出:

nat$ ruby file2.rb 
file2.rb:4:in `<class:Foo>'

That's what you want, right?

这就是你想要的,对吧?

#2


2  

Both $0 and __FILE__ will be useful to you.

$ 0和__FILE__都对你有用。

$0 is the path of the running application.

$ 0是正在运行的应用程序的路径。

__FILE__ is the path of the current script.

__FILE__是当前脚本的路径。

So, __FILE__ will be the script or module, even if it's been required.

所以,__ FILE__将是脚本或模块,即使它是必需的。

Also, __LINE__ might be useful to you.

此外,__LINE__可能对您有用。

See "What does __FILE__ mean in Ruby?", "What does if __FILE__ == $0 mean in Ruby" and "What does class_eval <<-“end_eval”, __FILE__, __LINE__ mean in Ruby? for more information.

请参阅“Ruby中__FILE__的含义是什么?”,“如果__FILE__ = = 0 0在Ruby中意味着什么”和“什么使用class_eval << - ”end_eval“,__ FILE __,_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _