Ruby Under a Microscope

Buy Now
Learning Ruby Internals Through Experiment

Have You Ever Wondered How Ruby Works Internally?

Everyone knows that Ruby is a powerful language. Its dynamic nature allows you to concisely write the code you need to actually solve a problem instead of burying yourself under mountains of boilerplate. You don't have to fight it; you just get to enjoy the ride.

But how is the magic created? And can you trust it? After all, MRI is written in C - a statically typed, compiled language which is pretty much the antithesis of Ruby. What dark voodoo is breathed into C to enable Ruby's features - closures, metaprogramming, and so much more?

Find out with Science!

Ruby Under a Microscope will guide you through the internals of some of Ruby's most-used facets. Using experimentation, theory, and two truckloads of diagrams, you'll clearly see how Ruby is implemented.

Here's just one of the diagrams out of the two truckloads.
Did you think that was a exaggeration? Well, it isn't!

Here's a science fact for you: before they understood the nature of combustion, Enlightenment chemists believed fire resulted from the release of a negative-weight particle called "phlogiston". Then Antoine-Laurent Lavoisier knocked that theory on its derrière by discovering oxygen and proving how combustion really works.

This book will offer you the same clarity. Instead of pointing at a Ruby block and yelling, "Phlogiston!", you'll be able to lucidly explain how Ruby copies variables from the stack to the heap in order to allow access to them even after a function has returned.

Have no clue what that means? Well, that's exactly why you need to read this book.

And good news: you don't need to know a lick of C! If you can't tell your pointers from your pointers to pointers, then have no fear. Ruby Under a Microscope is accessible to anyone with an interest in learning how Ruby works internally.

People Already Love It

Here's what people are already saying about Ruby Under a Microscope:

Many people have dug into the Ruby source code but few make it back out and tell the tale as elegantly as Pat does in Ruby Under A Microscope! I particularly love the diagrams - and there are lots of them - as they make many opaque implementation topics a lot easier to understand, especially when coupled with Pat's gentle narrative. This book is a delight for language implementation geeks and Rubyists with a penchant for digging into the guts of their tools.
Peter Cooper (@peterc) — Editor of Ruby Inside and Ruby Weekly
going to proofread a draft of @pat_shaughnessy's "Ruby Under a Microscope", man this book was missing in the Ruby landscape, awesome content
Xavier Noria (@fxn) — Ruby Hero, Ruby on Rails Core Team Member
Pat Shaughnessy did a tremendous job writing THE book about Ruby internals. Definitely a must read, you won't find information like this anywhere else.
Santiago Pastorino (@spastorino) — WyeWorks Co-Founder, Ruby on Rails Core Team Member
I really enjoyed the book and now know have a far better understanding of both Ruby and CS - thanks. Your writing made very complex topics (at least for me) very accessible and I found the book hard to put down. Diagrams were awesome and already are popping in my head as I code. This is by far one of my top 3 favourite Ruby books written.
Vlad Ivanovic (@vladiim) — Digital Strategist @ MassMedia Sydney.
While I'm not usually digging into Ruby Internals, your book was an absolute awesome read. Best $20 I've spent in ages.
David Deryl Downey (@daviddwdowney) — Founder of CyberSpace Technologies Group

The Table of Contents

Below is the Table of Contents. You'll probably learn a few things just by reading it. You'll learn even more by reading the book - go ahead and buy it!

Still not convinced? Pat has posted one section of each of these chapters for free on his blog already. The free sections are linked below:

1. Tokenization, Parsing and Compilation 13
Tokens: the words that make up the Ruby language 15
Parsing: how Ruby understands the code you write 24
Compilation: how Ruby translates your code into a new language 43
Tokenization, parsing and compilation in JRuby 64
Tokenization, parsing and compilation in Rubinius 68
2. How Ruby Executes Your Code 74
YARV’s internal stack and your Ruby stack 76
Local and dynamic access of Ruby variables 88
How YARV controls your program’s execution flow 102
How JRuby executes your code 113
How Rubinius executes your code 117
3. Objects, Classes and Modules 122
What’s inside a Ruby object? 124
Deducing what’s inside the RClass structure 136
How Ruby implements modules and method lookup 149
Objects, classes and modules in JRuby 163
Objects, classes and modules in Rubinius 168
4. Hash Tables 171
Hash tables in Ruby 173
How hash tables expand to accommodate more values 180
How Ruby implements hash functions 189
Hash tables in JRuby 199
Hash tables in Rubinius 202
5. How Ruby borrowed a decades old idea from Lisp 204
Blocks: Closures in Ruby 206
Lambdas and Procs: treating functions as a first class citizen 219
Metaprogramming and closures: eval, instance_eval and binding 239
Closures in JRuby 253
Closures in Rubinius 256

Buy Now for $20 and $2 Goes to Charity

Are you ready to educate your brain with Ruby science? Then Buy Ruby Under a Microscope now for only $20.

And guess what - with every purchase, Pat will donate $2 to charity! ($1 to Boston Children's Hospital and another $1 to the Saint Rock Haiti Foundation)

Take Me to the Purchase Page Already!

About Pat

Pat Shaughnessy writes a blog about Ruby development here on this web site, patshaughnessy.net. Pat's articles and presentations have been featured multiple times on the Ruby Weekly newsletter, the Ruby5 podcast and the Ruby Show.

When he's not at the keyboard, Pat enjoys spending time with his wife and two kids. Pat is also a fluent Spanish speaker and travels frequently to Spain to visit his wife's family.

Page design and copy by Daniel Higginbotham. You might like his Mac app, Foreman