I am a visual learner. A picture is worth a thousand words.
When I started learning Ruby, I could not find a decent diagram that would just sum up the Ruby classes, modules, and their hierarchy.
So I made mine.
On an A4 sheet.
Here is the result (click for a full size view):
Most of Ruby’s guts at one glance: pretty neat, huh ?
- Modules are on the top left.
- The usual types are here: strings, arrays, hashes, numbers, “booleans” (actually
- The error hierarchy is on the right.
- Other classes are in the middle.
Note: To have a decent layout, I only drew 70% of the Ruby 2 Core classes. All of the important ones are there, except
ObjectSpace: when I noticed, it was too late. :–)
What can we “draw” from this ?
Well, this model is pretty much what we would have expected:
- Arrays are enumerable, numbers are comparable.
- “Everything is an object”, or more precisely, every box is this diagram is a BasicObject.
- Object includes the Kernel module, so Object has all the instance methods of Kernel, like
When you write Ruby code, you are inside a
mainobject, that’s why you can just write
puts 'Hello world'instead of
Kernel.puts 'Hello world'.
So, everything makes sense and is quite simple actually !
Except maybe the fact that a module is a class, and class inherits from module. Don’t worry, you will eventually wrap your head around it.
In addition to Ruby Core, the Ruby Standard Library contains many other very useful classes and modules, such as
This diagram does not explain the Ruby method lookup path, which takes eigenclasses, included modules and superclasses into account. I recommend reading this enlightening article from Practicing Ruby.