Demystifying Ruby ♦️ (3/3): Memory Shenanigans

How does Ruby do memory management ?

Garbage collection (GC) is an essential process in any programming language that manages memory for the programmer. In Ruby (this article is about the MRI), the garbage collector plays a crucial role in ensuring efficient memory usage and preventing memory leaks.

The Heap and the Stack

Memory in a Ruby program is divided into two main regions:

  • The Heap: This is where dynamically allocated memory lives. Objects created at runtime, such as strings, arrays, and hashes, are stored here. The garbage collector primarily operates on the heap to free up unused memory.
  • The Stack: This is where method calls, local variables, and control flow data are stored. The stack is managed automatically and follows a Last In, First Out (LIFO) structure. Each time a method is called, a new stack frame is created; when the method completes, its stack frame is removed.

Ruby’s garbage collector is responsible for:

[Read More]

Demystifying Ruby ♦️ (2/3): Objects, Objects everywhere

And a little bit of Metaprogramming

One of the most fundamental principles of Ruby is that everything in the language is an object. This object-oriented allows developers to think in terms of objects and their interactions rather than just functions and procedures.

In Ruby, every piece of data, whether it’s a number, string, array, or even a class itself, is treated as an object. This means that all these entities can have properties and behaviors, encapsulated within methods and attributes. Let’s explore this !

[Read More]

Demystifying Ruby ♦️ (1/3): It's all about threads

Is Ruby a true parallel language ?!

Ruby is a dynamic, interpreted, open-source programming language known for its simplicity, productivity, and its “human-readable” syntax. Ruby is often used in web development, particularly with the Ruby on Rails framework. It supports object-oriented, functional, and imperative programming paradigms.

The most known and used Ruby Virtual Machine is the Matz Ruby Interpreter (aka CRuby), developed by Yukihiro Matsumoto (aka Matz), the creator of Ruby. All other Ruby implementation such as JRuby, TruffleRuby and so on are out of the scope of this blog post.

[Read More]