Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Intro to the Ruby VM

Intro to the Ruby VM

An introductory look at how Ruby tokenizes, parses and compiles your code -- and why you should care.

Ashley Ellis Pierce

December 03, 2015
Tweet

More Decks by Ashley Ellis Pierce

Other Decks in Programming

Transcript

  1. “A VIRTUAL MACHINE (VM) IS AN OPERATING SYSTEM (OS) OR

    APPLICATION ENVIRONMENT THAT IS INSTALLED ON SOFTWARE WHICH IMITATES DEDICATED HARDWARE.” Source: http://searchservervirtualization.techtarget.com/definition/virtual-machine
  2. TOKENIZING: GOES THROUGH EACH CHARACTER AND CONVERTS THEM TO A

    SERIES OF TOKENS THAT RUBY UNDERSTANDS 10 -> tInteger
  3. ‣ SINCE RUBY 1.9, AFTER YOUR RUBY CODE IS PARSED

    AND TOKENIZED IT’S COMPILED TO BYTE CODE USING YARV. ‣ THIS IS VERY SIMILAR TO HOW JAVA WORKS, THE JVM COMPILES IT TO JAVA BYTE CODE. ‣ BEFORE 1.9 RUBY WAS BEING INTERPRETED TO C AND THEN THE C COMPILES TO MACHINE CODE.
  4. COMPILER: FROM SOURCE CODE TO MACHINE LEVEL CODE ONCE (UNTIL

    THE PROGRAM IS CHANGED) INTERPRETER: DOES SOME CONVERSION WORK EVERY TIME A STATEMENT OR FUNCTION IS EXECUTED
  5. PRE 1.9- *TRAVERSES* ABSTRACT SYNTAX TREE (AST) (KIND OF LIKE

    THE DOM) NOW - YARV COMPILES THAT AST TO YARV BYTECODE AND THEN RUNS IT. https://www.igvita.com/2008/12/11/ruby-ast-for-fun-and-profit/
  6. Shaughnessy, P. (2013). How Ruby Executes Your Code. In Ruby

    under a microscope: An illustrated guide to Ruby internals (1st ed., p. 360). No Starch Press.
  7. Shaughnessy, P. (2013). How Ruby Executes Your Code. In Ruby

    under a microscope: An illustrated guide to Ruby internals (1st ed., p. 360). No Starch Press.
  8. RESOURCES FOR OTHER LANGUAGES ▸ Python ▸ Also an Intermediate

    Language (Compiled and Interpreted) ▸ dis function to see CPython byte code ▸ https://docs.python.org/2/ library/dis.html ▸ Javascript ▸ Interpreted with optional JIT (just- in-time) compilation support. ▸ All modern day browsers use the JIT ▸ Capstone.js or similar tools can be used to disassemble code to byte code ▸ http://alexaltea.github.io/ capstone.js/
  9. SOURCES ‣ http://searchservervirtualization.techtarget.com/definition/virtual- machine ‣ Shaughnessy, P. (2013). Ruby under

    a microscope: An illustrated guide to Ruby internals (1st ed., p. 360). No Starch Press. ‣ Cooper, P. (2011, August 5). Using Ripper to See How Ruby Is Parsing Your Code. Retrieved December 2, 2015, from http:// www.rubyinside.com/using-ripper-to-see-how-ruby-is-parsing-your- code-5270.html ‣ https://www.igvita.com/2008/12/11/ruby-ast-for-fun-and-profit/