In Ruby is there a way to tell where a method is defined? I'm going through the ruby-guides and there is a line of code that reads Post.all
How can I tell where all
is defined?
在Ruby中有一种方法可以告诉方法的定义吗?我正在浏览ruby-guide,并且有一行代码可以读取Post.all如何判断所有内容的定义?
2 个解决方案
#1
17
If you want to know the file and line where the method is defined, use
如果您想知道定义方法的文件和行,请使用
Post.method(:all).source_location
It will give you [file, line]
or nil
if it's a C method.
如果它是C方法,它会给你[file,line]或nil。
#2
7
A method can be used via a Method object. Which at that point as an owner
attribute. So you can do something like this:
可以通过Method对象使用方法。在那时作为所有者属性。所以你可以这样做:
puts Post.method(:all).owner
That will tell you the module/class that defines the method.
这将告诉您定义方法的模块/类。
#1
17
If you want to know the file and line where the method is defined, use
如果您想知道定义方法的文件和行,请使用
Post.method(:all).source_location
It will give you [file, line]
or nil
if it's a C method.
如果它是C方法,它会给你[file,line]或nil。
#2
7
A method can be used via a Method object. Which at that point as an owner
attribute. So you can do something like this:
可以通过Method对象使用方法。在那时作为所有者属性。所以你可以这样做:
puts Post.method(:all).owner
That will tell you the module/class that defines the method.
这将告诉您定义方法的模块/类。