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.

B9723dc81eafa0294ce0a5e33a1fc584?s=128

Ashley Ellis Pierce

December 03, 2015
Tweet

Transcript

  1. INTRO TO THE RUBY VM @ASHLEYNELLIS Ashley Ellis

  2. WHAT THE HECK’S A VM?

  3. “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
  4. RUBY CODE -> BYTECODE

  5. YARV YET ANOTHER RUBY VIRTUAL MACHINE

  6. PREPARING YOUR CODE FOR YARV

  7. TOKENIZING: GOES THROUGH EACH CHARACTER AND CONVERTS THEM TO A

    SERIES OF TOKENS THAT RUBY UNDERSTANDS 10 -> tInteger
  8. PARSING: TOKENS ARE GROUPED INTO SENTENCES AND PHRASES THAT MAKE

    SENSE TO RUBY
  9. ‣ 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.
  10. COMPILER VS INTERPRETER

  11. 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
  12. RUBY IS STILL CLASSIFIED AS AN INTERPRETED LANGUAGE

  13. INTERMEDIATE REPRESENTATION COMBINES COMPILING AND INTERPRETING

  14. 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/
  15. None
  16. None
  17. 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.
  18. 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.
  19. SOO…WHY SHOULD I CARE?

  20. ‣ DEBUGGING WAT MOMENTS ‣ PERFORMANCE OPTIMIZATION ‣ METAPROGRAMMING ‣

    KNOWING WHEN TO USE WHAT LANGUAGE
  21. 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/
  22. 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/