Synopses & Reviews
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
Ruby is a powerful programming language with a focus on simplicity, but beneath its elegant syntax, the language is performing countless unseen tasks. Ruby Under a Microscope gives developers an inside, hands-on look at Ruby's core, using simple diagrams coupled with clear explanations. Author Pat Shaughnessy takes a scientific approach, laying out a series of experiments with Ruby code to demonstrate how things work. Shaughnessy's lucid illustrations will serve as a guide for developers writing Ruby programs. Developers of all stripes will find this a fascinating behind the scenes look at how programming languages work.
Covers Ruby 2.x
About the Author
Well known for his coding expertise and passion for the Ruby programming language, Pat Shaughnessy blogs and writes tutorials at https://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.
Table of Contents
; Advance Praise for Ruby under a Microscope; About the Author; Foreword; Acknowledgments; Introduction; Who This Book Is For; Using Ruby to Test Itself; Which Implementation of Ruby?; Overview; Chapter 1: Tokenization and Parsing; 1.1 Tokens: The Words That Make Up the Ruby Language; 1.2 Experiment 1-1: Using Ripper to Tokenize Different Ruby Scripts; 1.3 Parsing: How Ruby Understands Your Code; 1.4 Experiment 1-2: Using Ripper to Parse Different Ruby Scripts; 1.5 Summary; Chapter 2: Compilation; 2.1 No Compiler for Ruby 1.8; 2.2 Ruby 1.9 and 2.0 Introduce a Compiler; 2.3 How Ruby Compiles a Simple Script; 2.4 Compiling a Call to a Block; 2.5 Experiment 2-1: Displaying YARV Instructions; 2.6 The Local Table; 2.7 Experiment 2-2: Displaying the Local Table; 2.8 Summary; Chapter 3: How Ruby Executes Your Code; 3.1 YARV's Internal Stack and Your Ruby Stack; 3.2 Experiment 3-1: Benchmarking Ruby 2.0 and Ruby 1.9 vs. Ruby 1.8; 3.3 Local and Dynamic Access of Ruby Variables; 3.4 Experiment 3-2: Exploring Special Variables; 3.5 Summary; Chapter 4: Control Structures and Method Dispatch; 4.1 How Ruby Executes an if Statement; 4.2 Jumping from One Scope to Another; 4.3 Experiment 4-1: Testing How Ruby Implements for Loops Internally; 4.4 The send Instruction: Ruby's Most Complex Control Structure; 4.5 Calling Normal Ruby Methods; 4.6 Calling Built-In Ruby Methods; 4.7 Experiment 4-2: Exploring How Ruby Implements Keyword Arguments; 4.8 Summary; Chapter 5: Objects and Classes; 5.1 Inside a Ruby Object; 5.2 Experiment 5-1: How Long Does It Take to Save a New Instance Variable?; 5.3 What's Inside the RClass Structure?; 5.4 Experiment 5-2: Where Does Ruby Save Class Methods?; 5.5 Summary; Chapter 6: Method Lookup and Constant Lookup; 6.1 How Ruby Implements Modules; 6.2 Ruby's Method Lookup Algorithm; 6.3 Experiment 6-1: Modifying a Module After Including It; 6.4 Constant Lookup; 6.5 Lexical Scope in Ruby; 6.6 Experiment 6-2: Which Constant Will Ruby Find First?; 6.7 Summary; Chapter 7: The Hash Table: The Workhorse of Ruby Internals; 7.1 Hash Tables in Ruby; 7.2 Experiment 7-1: Retrieving a Value from Hashes of Varying Sizes; 7.3 How Hash Tables Expand to Accommodate More Values; 7.4 Experiment 7-2: Inserting One New Element into Hashes of Varying Sizes; 7.5 How Ruby Implements Hash Functions; 7.6 Experiment 7-3: Using Objects as Keys in a Hash; 7.7 Summary; Chapter 8: How Ruby Borrowed a Decades-Old Idea from Lisp; 8.1 Blocks: Closures in Ruby; 8.2 Experiment 8-1: Which Is Faster: A while Loop or Passing a Block to each?; 8.3 Lambdas and Procs: Treating a Function as a First-Class Citizen; 8.4 Experiment 8-2: Changing Local Variables After Calling lambda; 8.5 Summary; Chapter 9: Metaprogramming; 9.1 Alternative Ways to Define Methods; 9.2 Experiment 9-1: Who Am I? How self Changes with Lexical Scope; 9.3 Metaprogramming and Closures: eval, instance_eval, and binding; 9.4 Experiment 9-2: Using a Closure to Define a Method; 9.5 Summary; Chapter 10: JRuby: Ruby on the JVM; 10.1 Running Programs with MRI and JRuby; 10.2 Experiment 10-1: Monitoring JRuby's Just-in-Time Compiler; 10.3 Strings in JRuby and MRI; 10.4 Experiment 10-2: Measuring Copy-on-Write Performance; 10.5 Summary; Chapter 11: Rubinius: Ruby Implemented with Ruby; 11.1 The Rubinius Kernel and Virtual Machine; 11.2 Experiment 11-1: Comparing Backtraces in MRI and Rubinius; 11.3 Arrays in Rubinius and MRI; 11.4 Experiment 11-2: Exploring the Rubinius Implementation of Array#shift; 11.5 Summary; Chapter 12: Garbage Collection in MRI, JRuby, and Rubinius; 12.1 Garbage Collectors Solve Three Problems; 12.2 Garbage Collection in MRI: Mark and Sweep; 12.3 Experiment 12-1: Seeing MRI Garbage Collection in Action; 12.4 Garbage Collection in JRuby and Rubinius; 12.5 Copying Garbage Collection; 12.6 Generational Garbage Collection; 12.7 Concurrent Garbage Collection; 12.8 Experiment 12-2: Using Verbose GC Mode in JRuby; 12.9 Further Reading; 12.10 Summary;