Ruby Under a Microscope: An Illustrated Guide to Ruby Internals

Ruby Under a Microscope: An Illustrated Guide to Ruby Internals

Pat Shaughnessy

Language: English

Pages: 360

ISBN: 1593275277

Format: PDF / Kindle (mobi) / ePub

Ruby Under a Microscope: An Illustrated Guide to Ruby Internals

Pat Shaughnessy

Language: English

Pages: 360

ISBN: 1593275277

Format: PDF / Kindle (mobi) / ePub


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

Principles of Programming Languages (Undergraduate Topics in Computer Science)

Clojure High Performance Programming (2nd Edition)

Embedded Systems Circuits and Programming

Logic Programming with Prolog

Programming in C (4th Edition) (Developer's Library)

Netty in Action

 

 

 

 

 

 

 

 

 

 

If you’re interested in exploring JRuby’s compiler, look in the org.jruby.compile package in your copy of the JRuby source tree. The JRuby core team is also currently working on a new higher-level and less granular instruction set called “IR,” which will be specifically designed to represent Ruby programs. To learn more about the new IR instruction set see the article OSS Grant Roundup: JRuby’s New Intermediate Representation. 67 Ruby Under a Microscope Tokenization, parsing and compilation

it, if you prefer, saving some time later. Remember that MRI didn’t provide this feature: Ruby 1.9 and 2.0 always compile your code every time you run it. 68 Chapter 1: Tokenization, Parsing and Compilation But what makes Rubinius fascinating is the way that it implements Ruby using Ruby, or more precisely a combination of C, C++ and Ruby. I’ll have more examples of this later in other chapters, but for now let’s take a look at how Rubinius parses and compiles your code. Here’s the same

pass the string in as a method parameter – method arguments are essentially the same as local variables: def display_string(str) puts str end SP Variables Arguments The only difference between method arguments and local variables is that the calling code pushes the arguments onto the stack before the method calls even occurs. In this example, there are no local variables, but the single argument appears on the stack just like a local variable: 90 Chapter 2: How Ruby Executes Your Code def

with the same name shown on the right. However, inside the Rubinius C++ VM, the Object class is the common base class for all Rubinius objects, while BasicObject is actually a subclass of Object. This is the opposite of what we have in Ruby, where BasicObject is the superclass of Object. The ObjectHeader class, similar to the RBasic structure in MRI, contains some basic technical information Rubinius keeps track of for every object: 168 Chapter 3: Objects, Classes and Modules ObjectHeader

As you might guess, Ruby uses a hash table to hold the data you save in the Hash objects you create in your Ruby script. However, Ruby uses hash tables for many other reasons as well: it saves much of its own internal data in hash tables. Every time you create a method, Ruby inserts a new value in a hash table. Every time you create a constant, Ruby inserts a new value in a hash table. Ruby saves many of the special variables we saw in Experiment 2-2 in hash tables. As we saw in Chapter 3, Ruby

Download sample

Download