Ruby
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
191: BasicObject and the Root of the Hierarchy
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
ApiForwardermust forward every method call to the backend object, including methods that normally exist onObject(like#to_s,#inspect, and#class). - If the backend object does not respond to the method, the
ApiForwardershould return the string:"Method [method_name] not found on backend". - Since
BasicObjectdoesn't haveputs, you must useKernel.putsif 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"
There are no comments for now.