Slide 1

Slide 1 text

Ruby Interpreters By Katlyn Parvin 01001000 01100101 01101100 01101100 01101

Slide 2

Slide 2 text

MRI Matz’s Ruby Interpreter JRuby In particular... &

Slide 3

Slide 3 text

But before we get to tonight’s main event...

Slide 4

Slide 4 text

Interpreters Let’s learn a little about them! ● Why do we need an interpreter? ● How do they work? ?

Slide 5

Slide 5 text

Interpreter A computer program that interprets and translates source code into a format that the computer can understand.

Slide 6

Slide 6 text

To start at the beginning... we will have to travel back in time to the early 1940s ● Before the first interpreter in 1958 programs were written in assembly ● Assembly languages required a lot of knowledge about the hardware and were very error-prone ● Programs needed to be written in a way that the processor could understand

Slide 7

Slide 7 text

Ex. MIPS Assembly Language Load the data at location 32($s3) in memory to the local register $t0 Perform the addition of $t0 and $s2 and store the result in register $t0 Store the value of register $t0 to location 48($s3) in memory Ruby Program my_array[12] = 10 + my_array[8] Equivalent MIPS Program

Slide 8

Slide 8 text

High-level Languages Strongly abstracted from the implementation details of the computer C Java C++ Ruby

Slide 9

Slide 9 text

● Scans the entire program and translates it as a whole ● Fast execution time ● Compile once -> run many times Compiler ● Translates the program one statement at a time ● Slower execution time ● Faster analysis of the program ● Translates the source code every time it is run Interpreter Converting from high-level language or

Slide 10

Slide 10 text

Cool but, how do interpreters work?

Slide 11

Slide 11 text

Let’s see how things work inside of MRI

Slide 12

Slide 12 text

Example Code # Ruby Example x = 10 y = 5 puts x + y

Slide 13

Slide 13 text

Step 1 Tokenize Breakdown the ruby code into structured data # Ruby Example x = 10 y = 5 puts x + y

Slide 14

Slide 14 text

Step 2 Lexical Analysis Tagging each token as an identifier, operator or integer # Ruby Example x = 10 y = 5 puts x + y

Slide 15

Slide 15 text

Step 3 Build a Abstract Syntax Tree Representation of your program in memory # Ruby Example x = 10 y = 5 puts x + y

Slide 16

Slide 16 text

Step 4 Compile to Bytecode Run in YARV the ruby VM (Ruby 1.9+) # Ruby Example x = 10 y = 5 puts x + y

Slide 17

Slide 17 text

If you want to learn more about interpreters... I would highly recommend this talk by Ryan Davis From the 2013 Golden Gate Ruby Conf https://youtu.be/RPxvx9OkNic

Slide 18

Slide 18 text

But back to MRI and JRuby...

Slide 19

Slide 19 text

MRI Matz’s Ruby Interpreter - Written in C by the core Ruby team - Sets the standard for the Ruby Language - Well maintained with more paid core team members than any other Ruby Implementation - Consistent release cycle - Most likely what you are using to interpret Ruby

Slide 20

Slide 20 text

JRuby - Written in primarily java - Implemented on top of the JVM - Which means you can run Ruby code anywhere Java can run. - Ruby 2.3 Compatible - Actively being maintained - Supports true parallel multi-threading - Not all gems work with JRuby

Slide 21

Slide 21 text

Cool things about JRuby ● Use Java libraries ○ Great for companies that maintain Java codebases ○ Utilize 3rd party Java Libraries ■ Ex. Android development (ruboto) ● Use ruby in your Java files ○ Ex. utilize a ruby codebase within an existing Java app ● Allows for a lot more performance optimizations options ○ Tuning the JIT compiler ○ Threading

Slide 22

Slide 22 text

True Multi-Threading ● Ruby 1.8.7 the GIL (Global Interpreter Lock) prevent more than 1 thread of Ruby code to run at any time. ● Ruby 1.9+ can use multiple OS threads for I/O bound operations, but we cannot have parallel ruby processes ● JRuby can use true OS threads allowing us to utilize multiple CPUs http://www.restlessprogrammer.com/2013/02/multi-threading-in-jruby.html

Slide 23

Slide 23 text

Other interpreters to keep an eye on... Designed by Matz to be an embeddable implementation of Ruby Ruby interpreter written in RPython Implementation of Ruby that implements a generic runtime for dynamic languages on top of a Low Level Virtual Machine (LLVM) mRuby Topaz Rubinius Many more...

Slide 24

Slide 24 text

MRI Matz’s Ruby Interpreter JRuby In conclusion... &

Slide 25

Slide 25 text

Thank you! Katlyn Parvin Director of Engineering @ Mavenlink We are hiring! www.mavenlink.com/careers @KatlynParvin [email protected]