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

时间:2011-12-21 19:25:06

标签: ruby-on-rails ruby

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

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

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

想看看我在视图文件中可以使用哪些方法。具体来说,我想看看:has_many关联提供了哪些方法。 (我知道:has_many 提供什么,但想检查一下。)

8 个答案:

答案 0 :(得分:179)

以下将列出User类具有基础Object类没有的方法...

>> 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", ...

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

以下是我的User类具有的不在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.

请注意,由于在methods调用的结果中,用户类中定义的(多个)has_many关系而创建的方法

已添加请注意:has_many不会直接添加方法。相反,ActiveRecord机制使用Ruby method_missingresponds_to技术来动态处理方法调用。因此,这些方法未在methods方法结果中列出。

答案 1 :(得分:9)

Module#instance_methods

  

返回一个数组,其中包含接收器中public和protected实例方法的名称。对于模块,这些是公共和受保护的方法;对于一个类,它们是实例(而不是单例)方法。如果没有参数,或者参数为false,则返回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

答案 2 :(得分:5)

或者只是static void Main (string[] args) { int n; do { Console.Write ("Enter an integer : "); } while(!int.TryParse(Console.ReadLine (), out n)); //int has been entered, now do something else... } 仅返回该类中定义的方法。

答案 3 :(得分:4)

你可以做到

current_user.methods

为了更好的列表

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

答案 4 :(得分:3)

其中一个呢?

object.methods.sort
Class.methods.sort

答案 5 :(得分:1)

假设用户has_many帖子:

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

答案 6 :(得分:1)

阐述@ clyfe的回答。您可以使用以下代码获取实例方法的列表(假设您有一个名为&#34的对象类; Parser&#34;):

Parser.new.methods - Object.new.methods

答案 7 :(得分:1)

如果您正在查看按实例响应的方法列表(在您的情况下为@current_user)。根据ruby文档methods

  

返回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.

或者,您还可以检查方法是否可以在对象上调用?

@current_user.respond_to?:your_method_name

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

@current_user.methods - @current_user.class.superclass.new.methods #methods that are available to @current_user instance.
相关问题