Mobbing in ruby

What is the class of Ruby BasicObject?

sigu
podiihq
Published in
4 min readMar 27, 2018

--

What really is the class of BasicObject in Ruby? BasicObject is the parent class of all classes in Ruby. It’s an explicit blank class.

Today we were mobbing with friends from Mombasa (Raycee Mwatela and Rahma Halane)going through chapter 3 of the Well Grounded Rubyist book. Lets quickly dive into our discoveries.

Create a class on the irb

class Person
end

What happens when you ask the class Person for its class? What output do you expect? Class , right? Yeah you got it right

Class of our class we have created

Let’s get some basics again, did you know that the class Class inherits from Module ? In ruby we can ask the class for its super class. So superclass of Class is Module

Person is of class Class , the superclass of class Class is module so we can conclude that superclass of Person is Module, right?

Naaaaahhhh, its super class is Object. Shocked?, yeah you are not alone

Superclass of person class

Hold your thoughts right there. Lets move on to the next section, this will be cleared up soon :-)

Ruby lets you ask for ancestors of classes

We never knew this was a thing, so our Person class can list all its ancestors, whoever they have inherited from.

ancestors from the irb
ancestors from the pry session

Notice that there is an extra PP::ObjectMixin ancestor from the pry. Seems like something pry adds to enable it pretty print its code.

The class of Object

Ruby comes with an Object that you can use to create new objects, remember when our Person class told us their superclass is Object ?

This is the object we are talking about. What is its class? Take a guess….. Object ? Module ?

Naaahhhhhhh. Its class is Class

I know, that if messed up big time.

What is the superclass of Object? Is it Module ? or is it Class ? It is actually BasicObject , this now brings us to our title. What is the class of BasicObject?

Allow me to explain everything up to this point

I had to look for support online so I rushed to this stackoverflow answer to get some guidance on wtf is happening here

Every class in ruby has a superclass, Basic object being the topmost cannot have a superclass to it returns nil if asked for its superclass

Class is a just another class

Superclass of BasicObject is nil since it is the topmost in the inheritance heir achy. What about the class of BasicObject ? We took a while thinking and making guesses on this our answers being nil , BasicObject , Object etc

The class of BasicObject is Class , oooh nooo. Not again!!!

class of basicObject is class

Read the diagram below as String is an instance of Class , Integer is an instance of Class , Object is an instance of Class, BasicObject is an instance of Class

So from our previous discussion, the superclass of Class is Module but the class of Module is Class. The paradox never ends but I hope this clears up the air a bit

Conclusion

Whenever you write a new class in your app, you are just instantiating a new object whose class is Class

For more information about the relationship between Class , BasicObject Object , Module read the following stackoverflow answer. I will write a full blog about it later

We learned a lesson which in Swahili we say Tulijua hatujui — Translation: We thought we knew about objects and classes in ruby but realized we did not have as much knowledge

Also on this mob we had Kevin ochieng, Gertie Getty and Musa Omondi

--

--

sigu
podiihq

Software application developer, found new craze in functional programming.