Archive for the 'MRI' Category

Modules Part II - Strange Brew

« 4 February 2008 | 19:23 | patterns/idiom/style, Ruby Internals, MRI | Comments Off »

This post continues to look at Ruby Modules and their properties.
It’s possible to mix a Module into a second Module which can then be mixed into a class. The result is that an entire Module chain is moved into the a classes inheritance chain with one call to include.

module OneStrand
end

module TwoStrand
include OneStrand
end

class […]



Modules Part I - Enter the Include Class

« 25 November 2007 | 19:50 | Ruby Internals, MRI | 4 Comments »

Modules in Ruby serve two purposes. One is to provide namespace scope for code organization seen in the syntax Test::Unit::TestCase, where TestCase is a class defined inside of the Unit module, which is in turn defined within the Test module. The second purpose of modules is to provide a method for code reuse […]



Class Methods

« 19 October 2007 | 11:34 | patterns/idiom/style, Smalltalk, Ruby Internals, MRI | 2 Comments »

In my last post I wrote that providing a home for class methods was the raison d’être of the Metaclass. So why go through all the trouble? What are class methods good for? Four uses worth exploring are Object Creation, Annotations, DSL’s, and removal of structural duplication
This post is about Ruby class […]



The Metaclass

« 5 October 2007 | 9:34 | Ruby Internals, MRI | 6 Comments »

If you search around the web a bit, you are sure to find the Ruby Metaclass defined as “The singleton class of a class.” While this definition is accurate, it doesn’t contribute to an understanding of why Metaclasses exist. It is also guilty of underplaying some notable difference between Metaclasses and “normal” Singleton […]



Real Class

« 28 September 2007 | 8:23 | Smalltalk, JRuby, Ruby Internals, MRI | 5 Comments »

It interesting that Ruby, a language which allows you to so easily bypass encapsulation, encapsulates it’s own internals to the point of occasional prevarication. Heres an example…

pirate = Object.new
def pirate.speak
“Yarrrr”
end
pirate.class == Object.new.class
#=> true

In my last post I explained how the code above forces the interpreter to assign a newly created Singleton class to the […]



The Singleton Class

« 21 September 2007 | 9:47 | Ruby Internals, MRI | Comments Off »

There’s no shortage of literature on singleton classes, but it’s rare to see an explanation that covers implementation. This is unfortunate, as the implementation is not complex and familiarity with it leads to a more nuanced understanding.
A good way to look at singleton classes is to ask the question, “What is it that Ruby […]



Method Dispatch

« 14 September 2007 | 9:56 | Ruby Internals, MRI | Comments Off »

A good understanding of method dispatch can help in deciphering some of the odder corners of the Ruby object model. Luckily, method dispatch is not only straight forward, but entirely consistent for “normal” methods, singleton methods, and module methods.
In Matz’s Ruby Interpreter (MRI), method bodies are stored in a struct devoted to representing […]