Reason #94 • April 4th, 2026

Object#methods and friends for method introspection

Ruby provides many powerful introspection capabilities. One of them is the ability to list available methods on an object via Object#methods and friends:

Ruby
class MyClass
  def my_method
    "hello"
  end

  protected

  def my_protected_method
    "hello"
  end

  private

  def my_private_method
    "world"
  end
end

instance = MyClass.new

# #methods lists all public and protected methods, including inherited ones
instance.methods
# => [:my_method, :my_protected_method, :to_enum, :enum_for, :singleton_class, ...]

# #public_methods lists only public methods
instance.public_methods
# => [:my_method, :to_enum, :enum_for, :singleton_class, ...]

# #protected_methods lists only protected methods
instance.protected_methods
# => [:my_protected_method]

# #private_methods lists only private methods
instance.private_methods
# => [:my_private_method, :sprintf, :format, ...]

# By passing `false` to `public_methods` and `private_methods` we can exclude inherited ones:
instance.public_methods(false)
# => [:my_method]
instance.private_methods(false)
# => [:my_private_method]
    

All in all, these are very useful for IRB session debugging and exploration as well as metaprogramming.

History

These methods were added iteratively during Ruby's 1.1 era and properly released in Ruby 1.2.

The ability to exclude inherited methods by passing false to public_methods and private_methods was added in Ruby 1.8, released in 2003.

The most likely candidate for inspiration is Smalltalk, which provided similar introspective capabilities by exposing method dictionaries.

Reason #95 ?