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

Table of Contents

Foreword by Aaron Pattersonxv
Chapter 1: Tokenization and Parsing3
Chapter 2: Compilation31
Chapter 3: How Ruby Executes Your Code55
Chapter 4: Control Structures and Method Dispatch83
Chapter 5: Objects and Classes105
Chapter 6: Method Lookup and Constant Lookup133
Chapter 7: The Hash Table: The Workhorse of Ruby Internals167
Chapter 8: How Ruby Borrowed a Decades-Old Idea from Lisp191
Chapter 9: Metaprogramming219
Chapter 10: JRuby: Ruby on the JVM251
Chapter 11: Rubinius: Ruby Implemented with Ruby273
Chapter 12: Garbage Collection in MRI, JRuby, and Rubinius295

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.