Implementing Ruby

Personal experiments.

Created by @ajlopez

Using reveal.js


A dynamic language

  • Smalltalk Influence
  • Also Python, Perl, Lisp, ...
  • Dynamic Type System
  • Memory Management

Only one Ruby?

  • Ruby
  • JRuby
  • IronRuby
  • Rubinius
  • MacRuby
  • HotRuby
  • mruby
  • ...


The easy approach

How Do Interpreters Work?

  • Read some source code
  • Build an AST (Abstract Syntax Tree) by parsing the source code
  • Evaluate the program by walking over the AST and performing instructions
Compilers for free
## AST ```ruby x = 2; y = x * 3 ``` ![AST](images/ast01.png)


  • Matz's Ruby Interpreter
  • Also called CRuby
  • de facto reference


How Do Compilers Work?

  • Read some source code
  • Build an AST by parsing the source code
  • Generate target code by walking the AST and emitting instructions
Compilers for free
## Virtual Machine, Bytecodes ```js x = 2; y = x * 3 ``` ![AST](images/bc01.png)


  • YARV (Yet another Ruby VM)
  • The official Ruby implementation for Ruby 1.9

  • Designed for concurrency
  • Native threads to run Ruby code on all the CPU cores
  • LLVM-based just-in-time (JIT) native machine code compiler

An Implementation

Ruby in C#


  • Modules and Classes
  • Class Hierarchy
  • Namespaces
  • Singleton Classes
  • Garbage Collector
  • Load .Dlls

Lexer and Parser

Everything is a Value

To be evaluated in Context

Test-Driven Development

Put your money where your mouth is!

## Bootstrap ```csharp public Machine() { this.requirepaths.Add("."); var basicobjectclass = new DynamicClass("BasicObject", null); var objectclass = new ModuleObject("Object", basicobjectclass); var moduleclass = new ModuleClass(objectclass); var classclass = new ClassClass(moduleclass, objectclass); this.rootcontext.SetLocalValue("BasicObject", basicobjectclass); this.rootcontext.SetLocalValue("Object", objectclass); this.rootcontext.SetLocalValue("Module", moduleclass); this.rootcontext.SetLocalValue("Class", classclass); basicobjectclass.SetClass(classclass); objectclass.SetClass(classclass); moduleclass.SetClass(classclass); classclass.SetClass(classclass); // ... ``` To be SIMPLIFIED!!

Things to Explore

The End

BY Angel 'Java' Lopez / / @ajlopez