Black Bytes
Help more people learn by sharing this post!

Exploring MRI Source Code

If you have been using Ruby for a while you are probably curious about how some things work under the hood.

One way to dig deep into Ruby internals is by reading the source code that makes it work. Even if you don’t know C, you can still pick up some interesting things.

A great way to navigate through the code is by using a ‘cross reference’ site like:

When you open a source file in there, you will be able to click on identifiers, like function & variable names, which will take you to the search function and make it easy for you to find where a function is being defined so you can learn more about it.

Exploring Core Classes

Most of your exploration will happen in the root folder. That’s where you will find the source code for all the core classes like Object in object.c or Array in array.c.

Let’s take a look at hash.c, if you scroll all the way down to line 3500 you will see some names that should be familiar.

Let’s start with this:

In this line, the Hash class is being defined by the rb_define_class function. The second argument is the superclass for this class.

If you want to learn how the process of defining a class works you can click on rb_define_class and then on the link that says ‘function definition’.

exploring mri source code

The first part from rb_define_class checks if the class has already been defined.

Inside that if block, Ruby does some sanity checks, like making sure that the thing that is already defined is a class.

If the class is not already defined then it defines it like this:

You could read the definition for all these methods, but I think they are pretty self-explanatory.

In st_add_direct, the ‘st’ part means ‘symbol table’, and this is just a hash table. The rb_const_set function sets a constant on the Object class, this will make the class available everywhere.

And rb_class_inherited calls the inherited method of the superclass, you can find the documentation for this method here.

The next section of the code is composed of method definitions. MRI uses rb_define_method to do that.

Here is an example:

The first argument is the class on which this method is being defined, the second argument is the method name, the third argument is the C function which actually implements this method, and the last argument is the number of arguments this Ruby method requires, a negative value indicates optional arguments.

The rb_define_singleton_method function is used to define a class method.

The body of rb_define_singleton_method is just one line of code:

If you want to continue exploring, a good file to take a look at is object.c.

Exploring The Standard Library

Ok, that’s enough C for today, how about reading some Ruby code? The Ruby Standard Library is written in Ruby and you can find it under the /lib directory.

The standard library contains things like OpenStruct, Base64 encoding and the Set data structure.

A set is similar to an array, but with the special property that all elements are unique. In other words, a set contains no duplicates.

How does that work? Are there any fancy algorithms behind this?

If we take a look at set.rb you will quickly discover that this is backed by a Hash object.

So if you add a duplicated element, there is no need to check if it already exists, it will just overwrite the old one.

Exploring Rubinius

Another way to explore Ruby’s source code is by taking a look at alternate implementations like Rubinius.

Rubinius code is organized in a different way than MRI, so for this I like to use Github and the ‘find file’ feature.

github find file

If you want to learn more about Enumerable then you just type ‘enumerable’ and you will see all the related files.


As you have seen, you can learn how Ruby does things under the hood without much effort. Go ahead and explore on your own and let everyone know what you discovered!

If you like this post don’t forget to join my newsletter, just drop your email in the form below and you will receive free updates & exclusive content. If you don’t do it now you will miss out on all the goodies!

Tyler Green says 8 months ago

Enjoyed the post, Jesus! It’s good to be prompted to dig into these things and even better to have a few starting points. Thanks!

    Jesus Castello says 8 months ago

    Thanks for reading :)

senthil says 8 months ago

super, I like blackbytes!

    Jesus Castello says 8 months ago

    Thank you!

tvinky says 8 months ago

Thanks for this post.
I was more scared of this Ruby implementation under the hood C code than I should. :)

Comments are closed