Unlocking Ruby’s Object Capabilities: A Deep Dive

January 31, 2025

Ruby is a dynamic and flexible language that allows developers to interact with objects in powerful ways. Understanding how to query an object’s capabilities can help us write cleaner, more robust, and maintainable code. Let’s explore the key techniques for interpreting object capability queries in Ruby.


Do you need more hands for your Ruby on Rails project?

Fill out our form! >>

Do you need more hands for your Ruby on Rails project?

1. Understanding Object Capability Queries

In Ruby, determining what an object can do is essential for writing dynamic and adaptable code. Some key methods include:

  • respond_to? – Checks if an object can respond to a specific method.
  • method_defined? – Checks if a method is defined in a class/module.
  • defined? – Determines if a variable, method, or constant exists.

2. Listing an Object’s Non-Private Methods

To get a list of all public and protected methods available to an object:

obj = "hello"
p obj.public_methods(false) # Lists public methods from `String`, excluding inherited ones.
p obj.methods(false)        # Lists both public and protected methods.

Passing true includes methods from ancestors.

3. Listing Private and Protected Methods

To retrieve private and protected methods:

p obj.private_methods(false)   # Lists private methods
p obj.protected_methods(false) # Lists protected methods

Private methods cannot be called with an explicit receiver (self), whereas protected methods can be called within the class hierarchy.

4. Handling Method Queries with method_missing and respond_to?

The method_missing technique allows us to handle undefined method calls dynamically.

class MyClass
  def method_missing(name, *args)
    puts "You tried to call: #{name} with args #{args}"
  end

  def respond_to_missing?(name, include_private = false)
    name.to_s.start_with?("custom_") || super
  end
end

obj = MyClass.new
obj.custom_greet(42) # => You tried to call: custom_greet with args [42]
p obj.respond_to?(:custom_greet) # => true

By implementing respond_to_missing?, we ensure respond_to? remains accurate.

5. Getting Class and Module Instance Methods

To list instance methods for a class or module:

p String.instance_methods(false)  # Lists instance methods of `String` (excluding inherited ones)
p String.public_instance_methods(false) # Only public instance methods
p String.private_instance_methods(false) # Only private instance methods

6. Listing an Object’s Singleton Methods

Singleton methods exist only on a specific object, not its class:

obj = "hello"
def obj.speak
  "I am unique!"
end
p obj.singleton_methods # => [:speak]

Final Thoughts

Understanding object capability queries in Ruby gives us a deeper control over our code. Whether you’re debugging, metaprogramming, or designing flexible APIs, these techniques will enhance your ability to work with objects effectively.

Have you used any of these techniques in your projects? Share your thoughts in the comments! 🚀

Leave a comment