Ruby methods Method Tutorial

In this section we will learn what the methods method is and how to use it in Ruby.

What is methods Method in Ruby?

The `methods` method is a built in method that is used to call on an object and see its list of instance methods!

Example: using methods method in Ruby

class Person 
    attr_accessor :first_name, :last_name
end 

prs = Person.new

puts prs.methods 

Output:

first_name=

first_name

last_name

last_name=

taint

tainted?

untaint

untrust

untrusted?

…

How does methods method work in Ruby?

Note that the `methods` method will return the entire instance methods of not just the class that the object is created from but also every instance variable defined in the parent classes as well.

For example, because implicitly every class in Ruby inherits from the Object class, then the entire instance methods of this class will be printed to the output stream as well if we call the `methods` method on top of an object.

Example: methods method in Ruby

puts "This is a string object".methods

Output:

allocate

superclass

new

<=>

<=

>=

==

===

autoload

included_modules

include?

ancestors

attr

attr_reader

attr_writer

attr_accessor

instance_methods

public_instance_methods

protected_instance_methods

private_instance_methods

constants

const_get

const_set

…
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies