Skip to Content
Course content

191: BasicObject and the Root of the Hierarchy

Click on the "Edit" button in the top corner of the screen to edit your slide content.

A few years ago, I was reviewing code for a junior dev who was building a wrapper for a legacy remote API. He wanted a "transparent proxy"—basically, an object where any method called on it would be forwarded as a command to a remote server. He used a standard Ruby class and implemented method_missing, which worked great for most things. But then he hit a wall: when he called proxy.to_s or proxy.inspect, the code didn't hit his method_missing logic. Instead, it just returned the standard Ruby object string. He spent half a day trying to "undefine" methods in Object before he realized he was fighting the hierarchy itself.

This is where BasicObject comes in. In Ruby, we usually think of Object as the root of everything. But if you dig deeper, Object actually inherits from BasicObject. While Object provides a wealth of utility methods (the ones that make Ruby feel like Ruby), BasicObject provides almost nothing. It is the closest thing you can get to a truly blank slate in the language.

Stripping Down to the Bare Bones

When you inherit from Object, you get a lot of "baggage." You get #to_s, #inspect, #is_a?, and a whole host of other methods that are useful for 99% of your classes. But if you're building a proxy or a DSL where you want every single call to be intercepted, that baggage is in your way. Object methods take precedence over method_missing.

By inheriting from BasicObject, you bypass all of that. Here is the difference in action:

class NormalProxy
  def method_missing(name, *args)
    "You called #{name}!"
  end
end

class BlankProxy < BasicObject
  def method_missing(name, *args)
    "You called #{name}!"
  end
end

normal = NormalProxy.new
blank = BlankProxy.new

puts normal.some_random_method # => "You called some_random_method!"
puts normal.to_s               # => "#<NormalProxy:0x0000...>" (Object#to_s wins)

puts blank.some_random_method  # => "You called some_random_method!"
puts blank.to_s                # => "You called to_s!" (BasicObject has no #to_s)

Notice how BlankProxy treats to_s just like any other method. It doesn't know what to_s is, so it immediately yields to method_missing. This is incredibly powerful for building tools that need to act as "ghosts" or intermediaries.

The Art of the Transparent Proxy

I usually recommend BasicObject when you're building a decorator or a wrapper that shouldn't leak its own identity. For example, imagine you're building a system that logs every interaction with a sensitive data object. If you use a standard class, your logger might accidentally trigger #hash or #display, and you'll lose the visibility of those calls.

With BasicObject, you can ensure a total capture. However, you have to be careful. Because you've stripped away almost everything, you can't even call puts inside a BasicObject method directly if you aren't careful, because puts is a method of Kernel, and BasicObject doesn't include the Kernel module.

The Debugging Trade-off

Here is the catch: BasicObject is a nightmare to debug. I've been there—trying to p a BasicObject only to realize that p relies on #inspect, which your BasicObject has likely intercepted via method_missing, leading to a stack overflow or a very confusing string.

If you need to inspect a BasicObject, you can't rely on the object itself to tell you what it is. You have to wrap it or use Kernel.instance_method to force a call. It's a trade-off. You get total control over the method dispatch, but you lose the safety net of the standard Ruby object toolkit. Use it when the transparency of the proxy is more important than the ease of debugging the proxy itself.




📋 Practical Task

Building a Transparent Method-Forwarding Proxy

Your task is to create a class called ApiForwarder that inherits from BasicObject. This class should act as a gateway to a "backend" object (which you will pass into the constructor).

Requirements:

  • The ApiForwarder must forward every method call to the backend object, including methods that normally exist on Object (like #to_s, #inspect, and #class).
  • If the backend object does not respond to the method, the ApiForwarder should return the string: "Method [method_name] not found on backend".
  • Since BasicObject doesn't have puts, you must use Kernel.puts if you need to print anything for debugging.

Testing your implementation:

# Setup a dummy backend
class Backend
  def to_s; "Backend's version of to_s"; end
  def hello; "Hello from the backend!"; end
end

backend = Backend.new
proxy = ApiForwarder.new(backend)

puts proxy.hello   # Expected: "Hello from the backend!"
puts proxy.to_s    # Expected: "Backend's version of to_s"
puts proxy.unknown # Expected: "Method unknown not found on backend"
Rating
0 0

There are no comments for now.

to be the first to leave a comment.