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.

51qtp4nzjrl-_sx258_bo1204203200_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):

Object ancestors

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?


For a closer look at “puts” in the Kernel module, check out this cool blog post by Vaidehi Joshi.





Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s