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.

Covers Ruby 2.x, 1.9 and 1.8

Chapter 1 Tokenization and Parsing

Tokens: The Words That Make Up the Ruby Language

Experiment 1-1: Using Ripper to Tokenize Different Ruby Scripts

Parsing: How Ruby Understands Your Code

Experiment 1-2: Using Ripper to Parse Different Ruby Scripts

Summary

Chapter 2 Compilation

No Compiler for Ruby 1.8

Ruby 1.9 and 2.0 Introduce a Compiler

How Ruby Compiles a Simple Script

Compiling a Call to a Block

Experiment 2-1: Displaying YARV Instructions

The Local Table

Experiment 2-2: Displaying the Local Table

Summary

Chapter 3 How Ruby Executes Your Code

YARV’s Internal Stack and Your Ruby Stack

Experiment 3-1: Benchmarking Ruby 2.0 and Ruby 1.9 vs. Ruby 1.8

Local and Dynamic Access of Ruby Variables

Experiment 3-2: Exploring Special Variables

Summary

Chapter 4 Control Structures and Method Dispatch

How Ruby Executes an if Statement

Jumping from One Scope to Another

Experiment 4-1: Testing How Ruby Implements for Loops Internally

The send Instruction: Ruby’s Most Complex Control Structure

Calling Normal Ruby Methods

Calling Built-In Ruby Methods

Experiment 4-2: Exploring How Ruby Implements Keyword Arguments

Summary

Chapter 5 Objects and Classes

Inside a Ruby Object

Experiment 5-1: How Long Does It Take to Save a New Instance Variable?

What’s Inside the RClass Structure?

Experiment 5-2: Where Does Ruby Save Class Methods?

Summary

Chapter 6 Method Lookup and Constant Lookup

How Ruby Implements Modules

Ruby’s Method Lookup Algorithm

Experiment 6-1: Modifying a Module After Including It

Constant Lookup

Lexical Scope in Ruby

Experiment 6-2: Which Constant Will Ruby Find First?

Summary

Chapter 7 The Hash Table: The Workhorse of Ruby Internals

Hash Tables in Ruby

Experiment 7-1: Retrieving a Value from Hashes of Varying Sizes

How Hash Tables Expand to Accommodate More Values

Experiment 7-2: Inserting One New Element into Hashes of Varying Sizes

How Ruby Implements Hash Functions

Experiment 7-3: Using Objects as Keys in a Hash

Summary

Chapter 8 How Ruby Borrowed a Decades-Old Idea from Lisp

Blocks: Closures in Ruby

Experiment 8-1: Which Is Faster: A while Loop or Passing a Block to each?

Lambdas and Procs: Treating a Function as a First-Class Citizen

Experiment 8-2: Changing Local Variables After Calling lambda

Summary

Chapter 9 Metaprogramming

Alternative Ways to Define Methods

Experiment 9-1: Who Am I? How self Changes with Lexical Scope

Metaprogramming and Closures: eval, instance_eval, and binding

Experiment 9-2: Using a Closure to Define a Method

Summary

Chapter 10 JRuby: Ruby on the JVM

Running Programs with MRI and JRuby

Experiment 10-1: Monitoring JRuby’s Just-in-Time Compiler

Strings in JRuby and MRI

Experiment 10-2: Measuring Copy-on-Write Performance

Summary

Chapter 11 Rubinius: Ruby Implemented with Ruby

The Rubinius Kernel and Virtual Machine

Experiment 11-1: Comparing Backtraces in MRI and Rubinius

Arrays in Rubinius and MRI

Experiment 11-2: Exploring the Rubinius Implementation of Array#shift

Summary

Chapter 12 Garbage Collection in MRI, JRuby, and Rubinius

Garbage Collectors Solve Three Problems

Garbage Collection in MRI: Mark and Sweep

Experiment 12-1: Seeing MRI Garbage Collection in Action

Garbage Collection in JRuby and Rubinius

Copying Garbage Collection

Generational Garbage Collection

Concurrent Garbage Collection

Experiment 12-2: Using Verbose GC Mode in JRuby

Further Reading

Summary

Title:

Ruby Under a Microscope

By:

Pat Shaughnessy

Publisher:

No Starch Press

Formats:

Print

Ebook

Safari Books Online

Print:

November 2013

Ebook:

November 2013

Pages:

360

Print ISBN:

978-1-59327-527-3

| ISBN 10:

1-59327-527-7

Ebook ISBN:

978-1-59327-561-7

| ISBN 10:

1-59327-561-7

Pat Shaughnessy

Well known for his coding expertise and passion for the Ruby programming language, Pat Shaughnessy blogs and writes tutorials at http://patshaughnessy.net/. He also develops Ruby applications at management consulting firm McKinsey & Co. Shaughnessy is a regular presenter on the Ruby conference circuit, and his articles and presentations have been featured in the Ruby Weekly newsletter, the Ruby5 podcast, and The Ruby Show.

If you're a relatively comfortable with Ruby, this book is fantastic at understanding what's going on under the hood. Which really, really helps at getting a mental picture of what the MRI system is doing, but also, why certain Ruby constructs - classes and modules and procs - work they way they do.

I found this invaluable when trying to consider architectural decisions, or build a ruby extension.

As a "language junkie" who has taught programming language principles, I always like to know how things work, not just how to use them. RUAM certainly satisfies that need, plus. Ruby sometimes surprises when porting code from one version to the next: RUAM will help, with explanations of some of the major internal changes between 1.8/1.9/2.0. RUAM is a big book, and covers a lot of ground, but still leaves room for more.

Great ressource if you are interrested in how a language like Ruby is really implemented. This book is best suited for the curious and those who want to get a grip on how things really work, not those who want to learn a better way to use Ruby.

The book starts with details on how MRI parse and compile your script to a internal representation, making it a great concrete application of various compiler techniques. They are presented in a clear and concrete way that is sometime missing from the classic books on compilers.

The middle of the book deals mainly with the MRI run time and how it keeps track of your data and where you are in your program. The various illustration are really clear and you can really understand what's going on. I had various 'ha ha !' moments where I finaly understood various limitations or behaviors of the language.

The end of the book deals with garbage collection and makes comparaisons with other runtimes like the JVM or Rubinius. While the comparaisions are pretty interresting, they may or may not be your prime issue, depending on how you use Ruby. If you are only using Ruby for small to medium scripts like me, it's not a real big deal.

I'm not sure reading this book is a must read for Rubists. For diving into Ruby, you should really start with the seminal 'The Ruby Programming Language' or 'Ruby cookbook' (even if the cookbook is now getting a little bit old, it remains largely valid).