Ruby is a powerful programming language with a focus on simplicity, but beneath its elegant syntax it performs countless unseen tasks.
Ruby Under a Microscope gives you a hands-on look at Ruby's core, using extensive diagrams and thorough explanations to show you how Ruby is implemented (no C skills required). Author Pat Shaughnessy takes a scientific approach, laying out a series of experiments with Ruby code to take you behind the scenes of how programming languages work. You'll even find information on JRuby and Rubinius (two alternative implementations of Ruby), as well as in-depth explorations of Ruby's garbage collection algorithm.
Ruby Under a Microscope will teach you:
How a few computer science concepts underpin Ruby's complex implementation How Ruby executes your code using a virtual machine How classes and modules are the same inside Ruby How Ruby employs algorithms originally developed for Lisp How Ruby uses grammar rules to parse and understand your code How your Ruby code is translated into a different language by a compiler No programming language needs to be a black box. Whether you're already intrigued by language implementation or just want to dig deeper into Ruby, you'll find Ruby Under a Microscope a fascinating way to become a better programmer.
When they say it's under a microscope, they are really not kidding--this is extremely detailed on what's going on behind the scenes in Ruby. Given the subject, I think the author did an admirable job of making the tone and descriptions as accessible as possible. It would probably be good to start off with some the talks he's given to get a general sense of his style (I've watched the one he has on ActiveRecord, and another on garbage collection that's basically the last chapter of this book).
Great book if you know pretty much nothing about compilers and want to get a taste. If you already know some things, but want to get a taste of Ruby internals, this is also a good book.
You really need to have a good grasp of Ruby before reading the book to get the most (or any) of its content. But sheesh, very interesting study of Ruby and especially the Metaprogramming and Garbage Collection chapters.
One of the best programming books I’ve read in awhile. Very low level and advanced. I tried to savor and digest it, but will definitely reread. It felt like a walkthrough on compilers, programming languages, and operating systems all over again.
Let's start with a disclaimer. I'm not a Computer Science major, nor did I complete a Computer Science course of study in school. I'm a software tester, and one that finds themselves frequently using programming languages of various stripes for various purposes. Ruby is one of the most popular languages in current use, and for many, it's a language that allows them to learn some basic terms, some programming constructs, and then lets them just use it. It's clean, it's elegant, it's almost simple. It's a language that invites the user to just go with it.
For some, though, there's that sense of curiosity... what is my Ruby program really doing? How can I see what the system is actually doing with my code? What's going on underneath the hood? If such explorations interest you, then "Ruby Under a Microscope" by Pat Shaughnessy tackles that subject handily.
A word of warning going in. This is not a general language book. You will not learn much about programming in Ruby here. You should have a decent understanding of what Ruby syntax looks like and how it works. Having said that, you don't need to have years of experience with Ruby to appreciate this book for what it does. It takes some key areas of the language, and through examples, some small programs, and a variety of tools, lets you see and understand what Ruby actually looks like up close and personal.
Chapter 1 focuses on how Ruby understand the text that you type into your Ruby program. Ruby converts your source code first into tokens, and then converts that input stream into an "abstract syntax tree”. Through tools like “Ripper”, you can see this process and watch it in something resembling natural language (well, kind of. It’s still debug code, but it’s a lot less intimidating than one might think.
Chapter 2 covers how Ruby compiles code. Wait, isn’t Ruby a “scripting language, no compiler required? With 1.8 and earlier, yes, but with 1.9 and up, Ruby is compiled just like many other languages we are familiar with. The difference? Ruby does it automatically. You never need to invoke the compiler. Ruby also has its own “virtual machine” (YARV, or "Yet Another Ruby Virtual Machine) that it compiles its bytecode for. Ultimately, the byte code for YARV is what we witness running.
Chapter 3 goes into greater detail about how YARV runs our code. By comparing the steps necessary to run a simple program, we can compare the time it takes to run a program in Ruby 1.8 (which doesn’t have a compile sequence, it just runs) and Ruby 1.9 and 2.0, which do have compile sequences. For simple and brief interactions, it actually looks like Ruby 1.8 performs better, but for longer runs with more iterations, 1.9 and 2.0 have a huge advantage over 1.8 by virtue of its compile cycle.
Chapter 4 focuses more attention on the virtual machine and how control structures and methods are handled within the YARV. If statements, for loops and calls to various methods demonstrate how ruby breaks down the instructions, as well as how it utilizes its internal calls to “jump” from one piece of code to another. Ruby categorizes methods into 11 types, and labels its built-in methods as CFUNC methods, meaning they are implemented in C. Ruby also uses a hash to keep track of the number of arguments, her labels and what their default values should be.
Chapter 5 looks at objects and classes, specifically, Ruby’s internal objects and classes. Each Ruby object is, ultimately, a class pointer paired with an array of instance variables, and everything in Ruby is an object. Several generic objects are shown, along with their C structures (RString, RArray, RRegexp, etc.), and demonstrate how they, likewise, are also simple combinations of a class pointer and instance variables. Classes are a little more involved. Each Ruby class can be defined as Ruby object (with its class pointer & instance variables) plus method definitions, attribute names, a constants table and a “superclass” pointer.
Chapter 6 brings us deeper into methods and constants, specifically how these aspects are found and represented. Ruby lets a programmer look at programs with two contrasting paradigms. Code can be organized through classes and super-classes, or it can be organized through "lexical scope". Which approach makes the most sense? it depends on what you want to have your program accomplish.
Chapter 7 gets into one of the key attributes of Ruby internals, the Hash Table. These are interesting data structures that allow a program to return values quickly, and to automatically increase in size as more elements are added. the chapter takes a deep dive into Ruby’s hash function and how it allows for elements to be accessed quickly.
Chapter 8 covers blocks, and how the Blocks concept in Ruby borrows from the “closure” idea first prosed in the Lisp language several decades back. Blocks can be defined as “a combination of a function and an environment to use when calling that function”. Using “lambda”, a block can become a data value that can be passed, saved, and reused.
Chapter 9 discusses Metaprogramming, a means to program in a way that code can inspect and change itself, dynamically. In other words, by referencing itself, your program can change itself! I’ll admit, this is one of the aspects of Ruby (or any language) that I have trouble getting my head around, and while I won’t claim to have mastery of these ideas after this chapter, I feel I have a little better feel for what’s happening.
Chapter 10 takes us into the Java realm and shows us Ruby implemented in Java, as opposed to how we’ve been interacting with it thus far in C. The flow is similar, but each Ruby script gets compiled into a Java executable, and then is physically run by the Java Virtual Machine. We see how “Jay” parses the lines of code (much the way Bison does for MRI). By monitoring Java’s Just In time Compiler, we can see which class and structures are called whenever we create a script and run it. We can also see where, by focusing on various “hot spots in our program and compiling them into Java, we can save time in key areas compared to C implemented MRI.
Chapter 11 introduces Rubinious, a version of Ruby implemented with Ruby. Well, it’s actually a virtual machine using C++ to run Ruby code. What makes it different is that, rather than relying on C or Java Structures for the built in classes, Rubinious does it with Ruby code. What does this mean? we can see how Ruby works internally without having to know C or Java. It’s all done in Ruby, and we can see how by reading the source code.
Chapter 12 explores Ruby’s Garbage Collection, and how it differs, and is similar, in MRI, JRuby and Rubinious. Garbage collection helps us with three processes; allocating memory for use by new objects, identifying which objects a program is no longer using, and reclaiming memory from unused objects. Various programs and examples demonstrate which objets are mapped where, and how to see when they are deallocated and their memory freed. Various algorithms for the various virtual machines are explored, but this is just a cursory overview of all the options and how they are implemented. Still, it’s an interesting view into a process that many of us take for granted because Ruby and many other languages basically let us take them for granted.
Bottom Line:
"Ruby Under a Microscope" does something fairly ambitious. It attempts to write a system internals book in a language that non computer scientists can readily understand. While there are numerous code snippets and examples to try and examine, the ability to look at the various Ruby internals and systems and see how they fit together can be accomplished by someone with general skills and basic familiarity with programming at the script level (which for many of us is as far as we typically get). An old saying says you can’t tell where yo hare going if you don’t know where you’ve been. Similarly, we can’t expect to get the most out of languages like ruby without having a more clear idea what’s happening under the hood. It’s entirely possible to work with Ruby and never learn some of this stuff, but having a guide like "Ruby Under a Microscope” opens up a variety of avenues, and does so in a way that will make the journey interesting and, dare I say it, even a little fun.
This book is a little dated at this point, but it's one of the better technical books I've read recently. It does a great job introducing you to the ins and outs of the Ruby programming language while also giving you a decent understanding of how it works internally. I've also never taken a compilers course, so this was a fun way to learn about how programming languages are implemented in the real world.
The book is written in a clear and understandable style. It always explains any concept it introduces and often gives a detailed example of how toy Ruby code would interact with those concepts, even if it walks a bit slowly through some of them. Even the chapters on JRuby and Rubinius, which I expected to be skippable, are useful because the book is structured to include details about how their design choices are shared with CRuby or how they are being incorporated back into the main CRuby implementation. It also stays high level enough that most things you learn relate to the features you use as a Ruby programmer rather than, say, the information you'd need to know to add features to the Ruby interpreter yourself, or the theory of interpreter design.
I wish there were a similar book about Javascript/V8! It's surprising there isn't one.
I read it after the YJIT paper, a few blog posts about Shopify's garbage collection optimizations, and some of the docs for the Prism parser, which help give a sense for a few of the things that have changed since the book was written. A lot of this book's writing still applies, though, so I think it's worth a read.
Why did I read the book? https://github.com/rubocop/rubocop/pu.... I wanted to draft this PR, get it merged, and actually understand how a linter works. Imagine if I actually had a Computer Science degree and didn't drop out to chase fast-pace ambition? I can't, that couldn't be me in those years. 😅
All that is needed is to understand how languages are implemented and how they work. What is the process of tokenization and parsing? What is Yet-Another-Ruby-Virtual Machine (YARV)? How can I work with Abstract-Syntax-Trees (AST)?
The book is a pretty light read, with many visual examples and experimentation sections where you can test the knowledge that Ruby Under a Microscope teaches.
I recommend this book to anyone who wants to become more knowledgeable in that area. Nota bene, it was written for Ruby 1.9-2.0. It's severely outdated in its practical language, and Ruby has evolved since then. Do double-check the implementation concerns. 😉
This book does an excellent job of explaining the C implementation of Ruby. It’s very well organized and takes you on a step by step journey through Ruby. It’s mainly focused on the C implementation but it does describe other implementations.
Ruby Under a Microscope will teach you:
- How a few computer science concepts underpin Ruby’s complex implementation - How Ruby executes your code using a virtual machine - How classes and modules are the same inside Ruby - How Ruby employs algorithms originally developed for Lisp - How Ruby uses grammar rules to parse and understand your code - How your Ruby code is translated into a different language by a compiler
That's pretty awesome for a Micro scoping Language.
Very interesting book - heavily (microscopically) focused on Ruby's implementation, but also a potential treasure for anyone wanting specifics on the implementation of a traditional lexer/parser/compiler or a VM. This was pretty brutal reading at times because so much of it is a ground-level examination of the actual YARV C implementation. But I can't imagine how this could be done better - it's heavily illustrated with diagrams and samples and experiments you can run. Does a great job of explaining the general concepts around hashing, garbage collection, metaprogramming, etc. I also liked the final chapter by YARV's creator, Koichi Sasada, which was an uplifting message to all developers to experiment and improve on Ruby.
Quite useful to have understood the inner workings of the programming language that I use every day. The author does a great job at explaining each facet of how the CRuby runtime works - from the stack to instances to classes to bindings, and more. Definitely requires a good understanding of operating systems and the Ruby language. This book offers a great in-depth understanding of how compilers and runtimes work. It was understandable from a compiler newbie like myself.
An interesting tome on the deep internals of Ruby as a language, mostly focused on the ruby VM
As somebody who works with the language daily this gave me some in-depth background about how it works and helps me better understand how the ideas in the language are structured
Its very unlikely to be widely applicable knowledge, but it helps ground some imagination and thoughts on the structure of complex DSLs
Highly recommend reading this if you're working with Ruby. The author does a great job explaining many university-level computer science concepts, and ties them in to Ruby examples (with sample code, benchmarks, and diagrams). Some of the information is a little dated now, but the overall theory remains relevant for Ruby and many other languages.
Ruby Under a Microscope covers advanced topics like YARV (Yet Another Ruby Virtual machine). Lots of time is spent converting ruby code into YARV code. It is great for understanding Virtual Machines but questionable on improving your ruby. It includes snippets of the C code behind MRI (Matz Ruby Interpreter). It has a chapter covering JRuby and Rubinius respectfully.
It is required reading if you want a low level understanding of MRI or want a handle on the C structures it uses to write your own C-extension. It is not that helpful for improving your Ruby skills. It got me interested in Rubinius to read over the source code. I'm also curious how C-extensions are implemented for MRI, Rubinius and JRuby. The sign of a good book is it inspires in depth questions. This meets the mark.
Ruby Under a Microscope covers advanced topics like YARV (Yet Another Ruby Virtual machine). Lots of time is spent converting ruby code into YARV code. It is great for understanding Virtual Machines but questionable on improving your ruby. It includes snippets of the C code behind MRI (Matz Ruby Interpreter). It has a chapter covering JRuby and Rubinius respectfully.
It is required reading if you want a low level understanding of MRI or want a handle on the C structures it uses to write your own C-extension. It is not that helpful for improving your Ruby skills. It got me interested in Rubinius to read over the source code. I'm also curious how C-extensions are implemented for MRI, Rubinius and JRuby. The sign of a good book is it inspires in depth questions. This meets the mark.
The book provides a really nice insight about how Ruby works behind the scenes and how many of its cool features like blocks are implemented. The author also analyzes the Ruby MRI implementation a lot from a performance perspective and identifies the strong and weak points. Overall, you can gain a lot of transferable knowledge about how dynamic programming languages work in general. The parts I enjoyed the most were discovering the dual stack mechanism, how the concept of closures and the ruby method lookup mechanism are implemented and, of course, the last part about how garbage collection.
Ever wonder how Ruby is implemented? You ever wanted to know all the different Garbage Collection Algorithms or the hashing function that Ruby uses? No? Then this book is not for you. But if you write Ruby code and want to understand the language better, then yeah, this is a great book.
To me, is not that I like these type of books, it's more like I NEED to have this understanding because I like to run the code in my head, and without knowing what's really going on it makes it tough to debug or optimize code that I wrote.
Ruby is my first real high level language, and as such I needed to know what's going on under the hood. It was fascinating.
Each chapter is only ~20-30 pages (with illustrations) and covers a big topic like tokenization/parsing, compilation, VM execution, method/constant lookup, garbage collection, etc. so there isn't a whole lot of depth in each chapter (some feel more rounded than others), but the book scratches a large surface.
Pat Shaughnessy shows how Ruby is implemented and often he verifies that his interpretation is correct with experiments and measurements.
The text is concise but the author provides enough context whenever there is a back reference, it makes easier to understand this reasonably complex subject.
As some reviewers pointed out, the best part of the book are the earlier chapters. I really enjoyed them. The last few chapters (fE on Hash tables) felt a bit like material was running out for the book. I also noticed that in the end I more skimmed over the 'how jruby/rubinius' sections than actually reading them.
It shows with nice graphs and explanations the internals of ruby. Also, it helped me to think that when I am using some kind of API there is a code below that is doing it work...
Definitely not your first Ruby book. It's a good read after you've been programming Ruby for a few years, and it's helpful to know a little bit of C first.
Great book to gain a very in depth understanding of Ruby and what is happening under the hood. If you are planning on writing C extensions I highly recommend this book.