How to list all methods for an object in Ruby?

后端 未结 8 1903
北荒
北荒 2020-12-07 08:44

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:



        
相关标签:
8条回答
  • 2020-12-07 09:09

    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:

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

    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.

    0 讨论(0)
  • 2020-12-07 09:18

    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"):

    Parser.new.methods - Object.new.methods
    
    0 讨论(0)
  • 2020-12-07 09:21

    Or just User.methods(false) to return only the methods defined within that class.

    0 讨论(0)
  • 2020-12-07 09:21

    You can do

    current_user.methods
    

    For better listing

    puts "\n\current_user.methods : "+ current_user.methods.sort.join("\n").to_s+"\n\n"
    
    0 讨论(0)
  • 2020-12-07 09:24

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

    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.

    @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.
    
    0 讨论(0)
  • 2020-12-07 09:25

    Suppose User has_many Posts:

    u = User.first
    u.posts.methods
    u.posts.methods - Object.methods
    
    0 讨论(0)
提交回复
热议问题