How to List All Methods for an Object in Ruby

How to list all methods for an object in Ruby?

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.

How to list all the methods defined in top self Object in Ruby?

The closest I could find is to call private_methods on the main object, with false as argument

Returns the list of private methods accessible to obj. If the all
parameter is set to false, only those methods in the receiver will be
listed.

def foo
"foo"
end

def bar
"bar"
end

def baz
"baz"
end

p private_methods(false)
# [:include, :using, :public, :private, :define_method, :DelegateClass, :foo, :bar, :baz]

If you omit the argument, you also get all the private methods defined in Kernel or BasicObject.

In order to refine the list further, you could select the methods defined for Object:

p private_methods(false).select{|m| method(m).owner == Object}
#=> [:DelegateClass, :foo, :bar, :baz]

Only :DelegateClass is left, because it is defined in the top-level scope, just like :foo, :bar and :baz.

Print all method names of a class in Ruby?

Try this one-liner:

puts 5.methods.sort

List all methods that an object respond_to?

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

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.

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.

How do I see all the methods/ properties in Object Class

#methods is the method you want. It simply returns an array of symbols, which are all the names of the methods that object responds to.

Object.new.methods

Or more readable in irb:

puts Object.new.methods.sort.to_yaml

Or for the class methods:

Object.methods

One caveat though, some objects allow methods that won't be listed here. Anything implemented with a hook into #method_missing won't show up. This includes a lot of ActiveRecord methods as well as other rails objects.

But so long as nothing tricky is going on, this is the list you seem to want.

Get list of a class' instance methods

You actually want TestClass.instance_methods, unless you're interested in what TestClass itself can do.

class TestClass
def method1
end

def method2
end

def method3
end
end

TestClass.methods.grep(/method1/) # => []
TestClass.instance_methods.grep(/method1/) # => ["method1"]
TestClass.methods.grep(/new/) # => ["new"]

Or you can call methods (not instance_methods) on the object:

test_object = TestClass.new
test_object.methods.grep(/method1/) # => ["method1"]

How do I list all objects created from a class in Ruby?

You can use the ObjectSpace module to do this, specifically the each_object method.

ObjectSpace.each_object(Project).count

For completeness, here's how you would use that in your class (hat tip to sawa)

class Project
# ...

def self.all
ObjectSpace.each_object(self).to_a
end

def self.count
all.count
end
end


Related Topics



Leave a reply



Submit