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?
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 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.
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.
going to proofread a draft of @pat_shaughnessy's "Ruby Under a Microscope", man this book was missing in the Ruby landscape, awesome content
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.
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.
While I'm not usually digging into Ruby Internals, your book was an absolute awesome read. Best $20 I've spent in ages.
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|
Are you ready to educate your brain with Ruby science? Then Buy Ruby Under a Microscope now for only $20.
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.