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

Dropping Down To The Metal™ (2018)

Godfrey Chan
October 06, 2018
97

Dropping Down To The Metal™ (2018)

As much as we love Ruby, when you need to be really close to the metal, you have no choice but to use JavaScript. This is why I developed the javascript gem to help you harness the raw power of your machines. (https://github.com/chancancode/javascript)

In this talk, we will examine the Ruby tricks and black magic hidden behind this ludicrous invention. Along the way, we will learn about how Ruby internally deal with variable lookups, method calls, scoping and bindings. Together, we will push the limits of the Ruby language, taking it to places Matz never ever envisioned!

Godfrey Chan

October 06, 2018
Tweet

Transcript

  1. Java ☕ public class HelloWorld { public static void main(String[]

    args) { System.out.println("Hello world!"); } } HelloWorld.java Terminal % javac HelloWorld.java % java HelloWorld Hello world! % % %
  2. Java ☕ public class HelloWorld { public static void main(String[]

    args) { System.out.println("Hello world!"); } } HelloWorld.java Terminal % mv HelloWorld.java hello.rb % ruby hello.rb ??? % % % hello.rb $ It works™ ) Syntax Error - Runtime Error
  3. Java ☕ public class HelloWorld { public static void main(String[]

    args) { System.out.println("Hello world!”); } } HelloWorld.java Terminal hello.rb $ It works™ ) Syntax Error - Runtime Error % ruby hello.rb hello.rb:2: syntax error, unexpected tIDENTIFIER, expecting ')' % %
  4. How Ruby Works J I don’t speak Russian. NP N

    VP VP V N V I don’t speak Russian S $ All Good
  5. How Ruby Works J Don’t Russian I speak. V N

    V N I Don’t speak Russian K Syntax Error ???
  6. How Ruby Works J Russian doesn’t speak me. NP N

    VP VP V N V me doesn’t speak Russian S 7 Semantic Error
  7. How Ruby Works J public class HelloWorld { public static

    void main(String[] args) { System.out.println("Hello world!”); } } HelloWorld.java Terminal hello.rb $ It works™ ) Syntax Error - Runtime Error % ruby hello.rb hello.rb:2: syntax error, unexpected tIDENTIFIER, expecting ')' % %
  8. console.log("Hello world!"); hello.js Terminal % mv hello.js hello.rb % ruby

    hello.rb ??? % % % $ It works™ ) Syntax Error - Runtime Error hello.rb JavaScript ☕
  9. console.log("Hello world!"); HelloWorld.java Terminal hello.rb $ It works™ ) Syntax

    Error - Runtime Error JavaScript ☕ % ruby hello.rb hello.js:1:in `<main>': undefined local variable or method `console’ for main:Object (NameError) % %
  10. console.log("Hello world!"); HelloWorld.java Terminal % ruby hello.rb hello.js:1:in `<main>': undefined

    local variable or method `console’ for main:Object (NameError) % % hello.rb JavaScript ☕
  11. class Console def log(*args) puts(*args) end end console = Console.new

    # Begin "JavaScript" console.log("Hello world!"); HelloWorld.java hello.rb JavaScript ☕ Terminal % ruby hello.rb Hello world! % %
  12. class Console def log(*args) puts(*args) end end console = Console.new

    # Begin "JavaScript" console.log("Hello world!"); HelloWorld.java hello.rb JavaScript ☕ Terminal % ruby hello.rb Hello world! % % class Console def log(*args) puts(*args) end end console = Console.new L
  13. def javascript yield end HelloWorld.java javascript.rb require "javascript" javascript do

    console.log("Hello world!"); end hello.rb DSL M Terminal % ruby hello.rb hello.rb:4:in `block in <main>': undefined local variable or method `console' for main:Object (NameError) % %
  14. class Console def log(*args) puts(*args) end end def console Console.new

    end def javascript yield end HelloWorld.java javascript.rb require "javascript" javascript do console.log("Hello world!"); end hello.rb DSL M Terminal % ruby hello.rb Hello world! % %
  15. class Console def log(*args) puts(*args) end end def console Console.new

    end def javascript yield end HelloWorld.java javascript.rb require "javascript" javascript do console.log("Hello world!"); end console.log("Hello world!"); hello.rb DSL M Terminal % ruby hello.rb Hello world! Hello world! % % L
  16. def javascript yield end HelloWorld.java javascript.rb require "javascript" javascript do

    self.console.log("Hello world"); end hello.rb DSL M Terminal % ruby hello.rb hello.rb:4:in `block in <main>': undefined local variable or method `console' for main:Object (NameError) % % Instance of Object
  17. class Console ... end class JavaScript def console Console.new end

    end def javascript(&block) JavaScript.new.instance_exec(&block) end HelloWorld.java javascript.rb require "javascript" javascript do console.log("Hello world!"); end console.log("Hello world!"); hello.rb DSL M Terminal % ruby hello.rb Hello world! hello.rb:4:in `block in <main>': undefined local variable or method `console' for main:Object (NameError) % % N self is instance of JavaScript O self is instance of Object
  18. class Console ... end class JavaScript def console Console.new end

    end def javascript(&block) JavaScript.new.instance_exec(&block) end HelloWorld.java javascript.rb require "javascript" javascript do console.log("Hello world!"); end hello.rb DSL M Terminal % ruby hello.rb Hello world! % %
  19. require "javascript" javascript do let message = "Hello world!"; console.log(message);

    end hello.rb Terminal $ It works™ ) Syntax Error - No Method Error ( Name Error % ruby hello.rb ??? % % Variables ℹ
  20. $ It works™ ) Syntax Error - No Method Error

    ( Name Error console.log("Hello world!"); hello.rb Terminal Variables ℹ % ruby hello.rb hello.rb:4:in `block in <main>': undefined method `let' for #<JavaScript:...> (NoMethodError) % %
  21. class Console ... end class JavaScript def console ... end

    def let(???) ??? end end def javascript ... end HelloWorld.java javascript.rb require "javascript" javascript do let message = "Hello world!"; console.log(message); end hello.rb Terminal % Variables ℹ
  22. class Console ... end class JavaScript def console ... end

    def let(*) # ¯\_(ϑ)_/¯ end end def javascript ... end HelloWorld.java javascript.rb require "javascript" javascript do let message = "Hello world!"; console.log(message); end hello.rb Terminal % ruby hello.rb Hello world! % % Variables ℹ
  23. require "javascript" javascript do let message; message = "Hello world!";

    console.log(message); end hello.rb Terminal $ It works™ ) Syntax Error - No Method Error ( Name Error % ruby hello.rb ??? % % Variables ℹ
  24. $ It works™ ) Syntax Error - No Method Error

    ( Name Error console.log("Hello world!"); hello.rb Terminal Variables ℹ % ruby hello.rb hello.rb:4:in `block in <main>': undefined method `message’ for #<JavaScript:...> (NoMethodError) % %
  25. class Console ... end class JavaScript def console ... end

    def let ... end def message # ¯\_(ϑ)_/¯ end end def javascript ... end HelloWorld.java javascript.rb Terminal % ruby hello.rb Hello world! % % Variables ℹ require "javascript" javascript do let message; message = "Hello world!"; console.log(message); end hello.rb
  26. class Console ... end class JavaScript def console ... end

    def let ... end def message # ¯\_(ϑ)_/¯ end end def javascript ... end HelloWorld.java javascript.rb Terminal % ruby hello.rb Hello world! % % Variables ℹ def message # ¯\_(ϑ)_/¯ end L require "javascript" javascript do let message; message = "Hello world!"; console.log(message); end hello.rb
  27. class Console ... end class JavaScript def console ... end

    def let ... end def method_missing(*) # ¯\_(ϑ)_/¯ end end def javascript ... end HelloWorld.java javascript.rb require "javascript" javascript do let message; message = "Hello world!"; console.log(message); end hello.rb Terminal % ruby hello.rb Hello world! % % Variables ℹ
  28. require "javascript" javascript do function hello() { console.log("Hello world!"); }

    hello(); end hello.rb Terminal $ It works™ ) Syntax Error - No Method Error ( Name Error % ruby hello.rb ??? % % Functions ⤵
  29. require "javascript" javascript do function hello() { console.log("Hello world!"); }

    hello(); end hello.rb Terminal $ It works™ ) Syntax Error - No Method Error ( Name Error % ruby hello.rb ??? % % Functions ⤵
  30. $ It works™ ) Syntax Error - No Method Error

    ( Name Error console.log("Hello world!"); hello.rb Terminal % ruby hello.rb % % * None of the above Functions ⤵
  31. class Console ... end class JavaScript def console ... end

    def let ... end def method_missing(*) # ¯\_(ϑ)_/¯ end end def javascript ... end HelloWorld.java javascript.rb require "javascript" javascript do function hello() { console.log("Hello world!"); } hello(); end hello.rb Terminal % Functions ⤵
  32. class Console ... end class JavaScript def ... end def

    function(*); end def method_missing(name, *, &block) if block_given? define_singleton_method(name, block) end end end def javascript ... end HelloWorld.java javascript.rb require "javascript" javascript do function hello() { console.log("Hello world!"); } hello(); end hello.rb Terminal Functions ⤵ % ruby hello.rb Hello world! % %
  33. require "javascript" javascript do function hello(name) { console.log(`Hello ${name}!`); }

    hello("world"); end hello.rb Terminal $ It works™ ) Syntax Error - No Method Error ( Name Error % ruby hello.rb ??? % % Arguments S Arguments
  34. require "javascript" javascript do function hello(name) { console.log(`Hello ${name}!`); }

    hello("world"); end hello.rb Terminal $ It works™ ) Syntax Error - No Method Error ( Name Error % ruby hello.rb ??? % % Arguments S Arguments