如何列出Ruby中对象的所有方法?

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

How do I list all the methods that a particular object has access to?

如何列出特定对象访问的所有方法?

I have a @current_user object, defined in the application controller:

我有一个在应用程序控制器中定义的@current_user对象:

def current_user
  @current_user ||= User.find(session[:user_id]) if session[:user_id]
end

And want to see what methods I have available to me in the view file. Specifically, I want to see what methods a :has_many association provides. (I know what :has_many should provide, but want to check that.)

想看看我在视图文件中有什么方法。具体来说,我想看看a:has_many关联提供了什么方法。(我知道:has_many应该提供,但要检查一下。)

7 个解决方案

#1


157  

The following will list the methods that the User class has that the base Object class does not have...

下面将列出用户类拥有的基本对象类没有的方法……

>> User.methods - Object.methods
=> ["field_types", "maximum", "create!", "active_connections", "to_dropdown",
    "content_columns", "su_pw?", "default_timezone", "encode_quoted_value", 
    "reloadable?", "update", "reset_sequence_name", "default_timezone=", 
    "validate_find_options", "find_on_conditions_without_deprecation", 
    "validates_size_of", "execute_simple_calculation", "attr_protected", 
    "reflections", "table_name_prefix", ...

Note that methods is a method for Classes and for Class instances.

注意,方法是类和类实例的方法。

Here's the methods that my User class has that are not in the ActiveRecord base class:

下面是我的用户类拥有的不在ActiveRecord基类中的方法:

>> User.methods - ActiveRecord::Base.methods
=> ["field_types", "su_pw?", "set_login_attr", "create_user_and_conf_user", 
    "original_table_name", "field_type", "authenticate", "set_default_order",
    "id_name?", "id_name_column", "original_locking_column", "default_order",
    "subclass_associations",  ... 
# I ran the statements in the console.

Note that the methods created as a result of the (many) has_many relationships defined in the User class are not in the results of the methods call.

注意,作为在User类中定义的(many) has_many关系的结果创建的方法不在方法调用的结果中。

Added Note that :has_many does not add methods directly. Instead, the ActiveRecord machinery uses the Ruby method_missing and responds_to techniques to handle method calls on the fly. As a result, the methods are not listed in the methods method result.

添加注意:has_many不直接添加方法。相反,ActiveRecord机制使用Ruby method_missing和responds_to技术动态地处理方法调用。因此,方法没有在方法方法结果中列出。

#2


8  

Module#instance_methods

模块# instance_methods

Returns an array containing the names of the public and protected instance methods in the receiver. For a module, these are the public and protected methods; for a class, they are the instance (not singleton) methods. With no argument, or with an argument that is false, the instance methods in mod are returned, otherwise the methods in mod and mod’s superclasses are returned.

返回一个数组,该数组包含接收者中公共和受保护实例方法的名称。对于模块,这些是公共和受保护的方法;对于类,它们是实例(而不是单例)方法。如果没有参数,或者有一个错误的参数,那么将返回mod中的实例方法,否则将返回mod和mod的超类中的方法。

module A
  def method1()  end
end
class B
  def method2()  end
end
class C < B
  def method3()  end
end

A.instance_methods                #=> [:method1]
B.instance_methods(false)         #=> [:method2]
C.instance_methods(false)         #=> [:method3]
C.instance_methods(true).length   #=> 43

#3


4  

You can do

你可以做

current_user.methods

For better listing

为了更好的清单

puts "\n\current_user.methods : "+ current_user.methods.sort.join("\n").to_s+"\n\n"

#4


3  

What about one of these?

这些怎么样?

object.methods.sort
Class.methods.sort

#5


1  

Suppose User has_many Posts:

假设用户has_many帖子:

u = User.first
u.posts.methods
u.posts.methods - Object.methods

#6


1  

To expound upon @clyfe's answer. You can get a list of your instance methods using the following code (assuming that you have an Object Class named "Parser"):

详细阐述@clyfe的回答。您可以使用以下代码获得实例方法的列表(假设您有一个名为“Parser”的对象类):

Parser.new.methods - Object.new.methods

#7


1  

If You are looking list of methods which respond by an instance (in your case @current_user). According to ruby documentation methods

如果您正在查看以实例响应的方法列表(在您的例子中是@current_user)。根据ruby文档方法

Returns a list of the names of public and protected methods of obj. This will include all the methods accessible in obj's ancestors. If the optional parameter is false, it returns an array of obj's public and protected singleton methods, the array will not include methods in modules included in obj.

返回obj的公共和受保护方法的名称列表。这将包括obj祖先中可访问的所有方法。如果可选参数为false,则返回obj的公共和受保护的单例方法的数组,该数组将不包含obj中包含的模块中的方法。

@current_user.methods
@current_user.methods(false) #only public and protected singleton methods and also array will not include methods in modules included in @current_user class or parent of it.

Alternatively, You can also check that a method is callable on an object or not?.

另外,您还可以检查方法在对象上是否可调用。

@current_user.respond_to?:your_method_name

If you don't want parent class methods then just subtract the parent class methods from it.

如果您不想要父类方法,那么只需从中减去父类方法。

@current_user.methods - @current_user.class.superclass.new.methods #methods that are available to @current_user instance.

#1


157  

The following will list the methods that the User class has that the base Object class does not have...

下面将列出用户类拥有的基本对象类没有的方法……

>> User.methods - Object.methods
=> ["field_types", "maximum", "create!", "active_connections", "to_dropdown",
    "content_columns", "su_pw?", "default_timezone", "encode_quoted_value", 
    "reloadable?", "update", "reset_sequence_name", "default_timezone=", 
    "validate_find_options", "find_on_conditions_without_deprecation", 
    "validates_size_of", "execute_simple_calculation", "attr_protected", 
    "reflections", "table_name_prefix", ...

Note that methods is a method for Classes and for Class instances.

注意,方法是类和类实例的方法。

Here's the methods that my User class has that are not in the ActiveRecord base class:

下面是我的用户类拥有的不在ActiveRecord基类中的方法:

>> User.methods - ActiveRecord::Base.methods
=> ["field_types", "su_pw?", "set_login_attr", "create_user_and_conf_user", 
    "original_table_name", "field_type", "authenticate", "set_default_order",
    "id_name?", "id_name_column", "original_locking_column", "default_order",
    "subclass_associations",  ... 
# I ran the statements in the console.

Note that the methods created as a result of the (many) has_many relationships defined in the User class are not in the results of the methods call.

注意,作为在User类中定义的(many) has_many关系的结果创建的方法不在方法调用的结果中。

Added Note that :has_many does not add methods directly. Instead, the ActiveRecord machinery uses the Ruby method_missing and responds_to techniques to handle method calls on the fly. As a result, the methods are not listed in the methods method result.

添加注意:has_many不直接添加方法。相反,ActiveRecord机制使用Ruby method_missing和responds_to技术动态地处理方法调用。因此,方法没有在方法方法结果中列出。

#2


8  

Module#instance_methods

模块# instance_methods

Returns an array containing the names of the public and protected instance methods in the receiver. For a module, these are the public and protected methods; for a class, they are the instance (not singleton) methods. With no argument, or with an argument that is false, the instance methods in mod are returned, otherwise the methods in mod and mod’s superclasses are returned.

返回一个数组,该数组包含接收者中公共和受保护实例方法的名称。对于模块,这些是公共和受保护的方法;对于类,它们是实例(而不是单例)方法。如果没有参数,或者有一个错误的参数,那么将返回mod中的实例方法,否则将返回mod和mod的超类中的方法。

module A
  def method1()  end
end
class B
  def method2()  end
end
class C < B
  def method3()  end
end

A.instance_methods                #=> [:method1]
B.instance_methods(false)         #=> [:method2]
C.instance_methods(false)         #=> [:method3]
C.instance_methods(true).length   #=> 43

#3


4  

You can do

你可以做

current_user.methods

For better listing

为了更好的清单

puts "\n\current_user.methods : "+ current_user.methods.sort.join("\n").to_s+"\n\n"

#4


3  

What about one of these?

这些怎么样?

object.methods.sort
Class.methods.sort

#5


1  

Suppose User has_many Posts:

假设用户has_many帖子:

u = User.first
u.posts.methods
u.posts.methods - Object.methods

#6


1  

To expound upon @clyfe's answer. You can get a list of your instance methods using the following code (assuming that you have an Object Class named "Parser"):

详细阐述@clyfe的回答。您可以使用以下代码获得实例方法的列表(假设您有一个名为“Parser”的对象类):

Parser.new.methods - Object.new.methods

#7


1  

If You are looking list of methods which respond by an instance (in your case @current_user). According to ruby documentation methods

如果您正在查看以实例响应的方法列表(在您的例子中是@current_user)。根据ruby文档方法

Returns a list of the names of public and protected methods of obj. This will include all the methods accessible in obj's ancestors. If the optional parameter is false, it returns an array of obj's public and protected singleton methods, the array will not include methods in modules included in obj.

返回obj的公共和受保护方法的名称列表。这将包括obj祖先中可访问的所有方法。如果可选参数为false,则返回obj的公共和受保护的单例方法的数组,该数组将不包含obj中包含的模块中的方法。

@current_user.methods
@current_user.methods(false) #only public and protected singleton methods and also array will not include methods in modules included in @current_user class or parent of it.

Alternatively, You can also check that a method is callable on an object or not?.

另外,您还可以检查方法在对象上是否可调用。

@current_user.respond_to?:your_method_name

If you don't want parent class methods then just subtract the parent class methods from it.

如果您不想要父类方法,那么只需从中减去父类方法。

@current_user.methods - @current_user.class.superclass.new.methods #methods that are available to @current_user instance.