a chronicle of my coding discoveries

Ruby’s Object Ancestors

Two weeks ago, I completed the Flatiron School web development immersive (woot!), and three weeks from now, I’ll be starting a position as an instructor at Flatiron, coaching students through Ruby fundamentals (among many other topics) that I was learning four months ago.

In the meantime, I’ve been taking a deeper dive into Ruby and reading The Well-Grounded Rubyist to make sure I’ve solidified the most foundational information in my mind.

That’s how I stumbled across Object ancestors again, and more specifically, became curious about Kernel.

Don’t know what Kernel is? Open up irb in your terminal and type Object.ancestors. This is what you should see (as long as you have ruby installed):

So, what is Kernel (and, while we’re at it, what is BasicObject)?

David Black first mentions Kernel in chapter 4, Modules and Program Organization, of The Well-Grounded Rubyist. This is a hint to answering the question I posed, because, first of all, Kernel is a module. As Black says, Kernel is “the module where the majority of the methods common to all objects live.”

Just like any other module, Kernel is mixed in to a class to extend that class’s functionality. More specifically, Kernel contains the instance methods such as integer, loop, rand, and puts, which every Ruby object has access to upon creation.

Meanwhile, BasicObject is a parent class. As the Ruby documentation plainly puts it, “BasicObject is the parent class of all classes in Ruby.” It only has a few methods, such as equals (“==”) and new.

So, essentially, when you create a new object in Ruby, what you’re doing is inheriting methods from BasicObject and then mixing in Kernel methods. Cool, right?