2011-12-21 66 views
72

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

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

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

,并希望看到什么方法我在视图文件必须提供给我。具体而言,我想查看一下:has_many关联提供的方法。 (我知道:has_many应该提供,但需要检查。)

+0

为了澄清,您希望可以在'@ current_user'上调用方法吗? – 2011-12-21 19:28:28

+0

@Dirk,欢迎来到stackoverflow!请记住“检查”最能回答您问题的答案。对于任何问题,您也可以提出任何答案,并认为您认为有用/有帮助。 – 2011-12-22 01:04:41

回答

141

下面将列出用户类有基类对象没有方法...

>> 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是Classes和Class实例的一种方法。

下面是我的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. 

注意,如用户类中定义的(多)的has_many关系的结果而创建的方法是methods调用的结果中不是

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

+1

hmm? “a”。方法工作正常。 – steenslag 2011-12-21 19:35:23

+2

虽然这可能不完整,因为某些方法仅在调用method_missing时创建(例如动态查找程序) – 2011-12-21 19:36:07

+1

@steenslag - 正确。我不好。固定。 – 2011-12-21 19:43:52

3

其中之一呢?

object.methods.sort 
Class.methods.sort 
8

Module#instance_methods

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

你可以做

current_user.methods 

为了更好的房源

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

假设用户的has_many帖子:

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

,阐述在@ clyfe的answe河你可以使用下面的代码的实例方法列表(假设你有一个名为“分析器”对象类):

Parser.new.methods - Object.new.methods 
1

如果你正在寻找的,其通过实例回应(在你的情况方法列表@当前用户)。根据ruby文档methods

返回obj的public和protected方法的名称列表。 这将包括所有在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. 
相关问题