列出对象respond_to的所有方法?

时间:2021-03-02 19:34:08

I have two models,

我有两个模型,

User 
Membership

The two have the following relationship with one another

两者之间的关系如下

user has_many :memberships

I've been trying to figure out where the build method resides, and how do i get it in a list of methods for the instance. Here is the output of the debugger that shows my delima

我一直在试图弄清楚构建方法位于何处,以及如何在实例的方法列表中获得它。这是显示delima的调试器的输出

(rdb:63) @user.memberships.respond_to?"build"
true

While the following is returning false, shouldnt it return true??

当以下返回为false时,它是否返回为true?

(rdb:63) @user.memberships.instance_methods.include?"build"
false

3 个解决方案

#1


9  

One point is that instance_methods takes an optional boolean parameter, indicating whether you want to see methods of the instances ancestors. In your case I think you want instance_methods(true).

一点是,instance_methods接受一个可选的布尔参数,指示您是否希望查看实例祖先的方法。在您的例子中,我认为您需要instance_methods(true)。

However, it appears that "build" is an autogenerated method, according to the documentation. Typically the autogenerated methods in ActiveRecord are implemented by overriding method_missing and handling calls to "methods" that don't actually exist. responds_to is also overridden so that the class will indicate that it responds to the correct calls. However, since those "methods" aren't actually defined, they won't show up in the instance_methods list.

然而,根据文档,“构建”似乎是一种自动生成的方法。通常,ActiveRecord中的自动生成方法是通过重写method_missing并处理对实际上不存在的“方法”的调用来实现的。responds_to也被重写,以便类将指示它响应正确的调用。但是,由于这些“方法”实际上没有定义,它们不会出现在instance_methods列表中。

Since the list of commands that a class can respond_to using method_missing is essentially infinite, I'm pretty sure there's no way to get the list. For example, an ActiveRecord model that has attributes a,b,c, and d will automatically respond to calls such as find_by_a_and_b and find_by_a_b_and_c and find_by_b_and_d and so forth, ad infinitum. There's no way to get a list of all of those possibilities.

由于使用method_missing的类可以respond_to响应的命令列表实际上是无限的,所以我确信没有办法得到这个列表。例如,具有属性a、b、c和d的ActiveRecord模型将自动响应诸如find_by_a_and_b、find_by_a_b_and_c和find_by_b_and_d等调用,直到无穷。没有办法列出所有这些可能性。

#2


5  

Please note that instance_methods returns an array of String or Symbol depending on the Ruby version.

请注意,instance_methods根据Ruby版本返回一个字符串或符号数组。

Ruby 1.8 returns an Array of String, Ruby 1.9 an Array of Symbol.

Ruby 1.8返回一个字符串数组,Ruby 1.9是一个符号数组。

In Ruby 1.8

在Ruby 1.8

"".respond_to?(:upcase)
# => true 
"".class.instance_methods.include?("upcase")
# => false
"".class.instance_methods.include?(:upcase)
# => false

In Ruby 1.9

在Ruby 1.9

"".respond_to?(:upcase)
# => true 
"".class.instance_methods.include?("upcase")
# => false
"".class.instance_methods.include?(:upcase)
# => true

Also, instance_methods must be called on the class, not on the instance.

而且,必须在类上调用instance_methods,而不是在实例上调用。

#3


5  

You could try:

你可以试试:

@user = User.first

@user.methods.grep /method_name/

However, I don't think you'll see 'build' or 'create' in a list. Most likely these methods are generated dynamically

但是,我认为您不会在列表中看到'build'或'create'。这些方法很可能是动态生成的

#1


9  

One point is that instance_methods takes an optional boolean parameter, indicating whether you want to see methods of the instances ancestors. In your case I think you want instance_methods(true).

一点是,instance_methods接受一个可选的布尔参数,指示您是否希望查看实例祖先的方法。在您的例子中,我认为您需要instance_methods(true)。

However, it appears that "build" is an autogenerated method, according to the documentation. Typically the autogenerated methods in ActiveRecord are implemented by overriding method_missing and handling calls to "methods" that don't actually exist. responds_to is also overridden so that the class will indicate that it responds to the correct calls. However, since those "methods" aren't actually defined, they won't show up in the instance_methods list.

然而,根据文档,“构建”似乎是一种自动生成的方法。通常,ActiveRecord中的自动生成方法是通过重写method_missing并处理对实际上不存在的“方法”的调用来实现的。responds_to也被重写,以便类将指示它响应正确的调用。但是,由于这些“方法”实际上没有定义,它们不会出现在instance_methods列表中。

Since the list of commands that a class can respond_to using method_missing is essentially infinite, I'm pretty sure there's no way to get the list. For example, an ActiveRecord model that has attributes a,b,c, and d will automatically respond to calls such as find_by_a_and_b and find_by_a_b_and_c and find_by_b_and_d and so forth, ad infinitum. There's no way to get a list of all of those possibilities.

由于使用method_missing的类可以respond_to响应的命令列表实际上是无限的,所以我确信没有办法得到这个列表。例如,具有属性a、b、c和d的ActiveRecord模型将自动响应诸如find_by_a_and_b、find_by_a_b_and_c和find_by_b_and_d等调用,直到无穷。没有办法列出所有这些可能性。

#2


5  

Please note that instance_methods returns an array of String or Symbol depending on the Ruby version.

请注意,instance_methods根据Ruby版本返回一个字符串或符号数组。

Ruby 1.8 returns an Array of String, Ruby 1.9 an Array of Symbol.

Ruby 1.8返回一个字符串数组,Ruby 1.9是一个符号数组。

In Ruby 1.8

在Ruby 1.8

"".respond_to?(:upcase)
# => true 
"".class.instance_methods.include?("upcase")
# => false
"".class.instance_methods.include?(:upcase)
# => false

In Ruby 1.9

在Ruby 1.9

"".respond_to?(:upcase)
# => true 
"".class.instance_methods.include?("upcase")
# => false
"".class.instance_methods.include?(:upcase)
# => true

Also, instance_methods must be called on the class, not on the instance.

而且,必须在类上调用instance_methods,而不是在实例上调用。

#3


5  

You could try:

你可以试试:

@user = User.first

@user.methods.grep /method_name/

However, I don't think you'll see 'build' or 'create' in a list. Most likely these methods are generated dynamically

但是,我认为您不会在列表中看到'build'或'create'。这些方法很可能是动态生成的