Dropping Down To The Metal™ (2018)

22bb3e56828870ee9a0dd93aeadbe04a?s=47 Godfrey Chan
October 06, 2018
63

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!

22bb3e56828870ee9a0dd93aeadbe04a?s=128

Godfrey Chan

October 06, 2018
Tweet

Transcript

  1. Dropping Down To The Metal™ ™

  2. None
  3. Java ☕

  4. 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! % % %
  5. 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
  6. 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 ')' % %
  7. How Ruby Works J ??? hello.rb Output Ruby Interpreter Compile

    Evaluate ???
  8. How Ruby Works J hello.rb Output Ruby Interpreter Compile Execute

    Syntax Error Runtime Error Parse
  9. How Ruby Works J I don’t speak Russian. NP N

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

    V N I Don’t speak Russian K Syntax Error ???
  11. 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
  12. How Ruby Works J hello.rb Output Ruby Interpreter Compile Execute

    Syntax Error Runtime Error Parse
  13. 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 ')' % %
  14. JavaScript

  15. JavaScript ☕ console.log("Hello world!"); hello.js Terminal % node hello.js Hello

    world! % %
  16. console.log("Hello world!"); hello.js Terminal % mv hello.js hello.rb % ruby

    hello.rb ??? % % % $ It works™ ) Syntax Error - Runtime Error hello.rb JavaScript ☕
  17. 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) % %
  18. console.log("Hello world!"); JavaScript ☕

  19. console.log("Hello world!"); Local variable or method named “console” JavaScript ☕

  20. console.log("Hello world!"); JavaScript ☕ Call the instance method “log” on

    “console”
  21. console.log("Hello world!"); JavaScript ☕ Optional parenthesis

  22. console.log("Hello world!"); JavaScript ☕ Method argument (string literal “Hello world!”)

  23. console.log("Hello world!"); JavaScript ☕ Optional semicolon

  24. console.log("Hello world!"); JavaScript ☕

  25. 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 ☕
  26. 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! % %
  27. None
  28. 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
  29. require "javascript" javascript do console.log("Hello world!"); end HelloWorld.java hello.rb JavaScript

    ☕ Terminal % ruby hello.rb Hello world! % %
  30. DSL Domain-specific Language M

  31. 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) % %
  32. 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! % %
  33. 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
  34. 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
  35. 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
  36. 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! % %
  37. None
  38. Variables ℹ

  39. 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 ℹ
  40. $ 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) % %
  41. let message = "Hello world!"; Variables ℹ

  42. let(message = "Hello world!"); Variables ℹ

  43. let(message = "Hello world!"); Variables ℹ Call the method named

    “let”…
  44. let(message = "Hello world!"); Variables ℹ …with this argument.

  45. let(message = "Hello world!"); Variables ℹ What is it?

  46. let(message = "Hello world!"); Variables ℹ Assignment Expression

  47. let(message = "Hello world!"); Variables ℹ

  48. let(message = "Hello world!"); Variables ℹ

  49. message = "Hello world!"; let(message); Variables ℹ

  50. message = "Hello world!"; let("Hello world!"); Variables ℹ

  51. 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 ℹ
  52. message = "Hello world!"; let("Hello world!"); Variables ℹ $ Q

  53. 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 ℹ
  54. None
  55. wait a second… J

  56. 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 ℹ
  57. $ 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) % %
  58. let message; message = "Hello world!"; console.log(message); Variables ℹ

  59. let(message); message = "Hello world!"; console.log(message); Variables ℹ

  60. let(message); message = "Hello world!"; console.log(message); Variables ℹ defined here

  61. let(message); message = "Hello world!"; console.log(message); Variables ℹ not local

    variable
  62. let(message); message = "Hello world!"; console.log(message); Variables ℹ method?

  63. let(message()); message = "Hello world!"; console.log(message); Variables ℹ

  64. 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
  65. 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
  66. 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 ℹ
  67. None
  68. Functions ⤵

  69. 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 ⤵
  70. 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 ⤵
  71. $ It works™ ) Syntax Error - No Method Error

    ( Name Error console.log("Hello world!"); hello.rb Terminal % ruby hello.rb % % * None of the above Functions ⤵
  72. function hello() { console.log("Hello world!"); } hello(); Functions ⤵

  73. function(hello() { console.log("Hello world!"); }); hello(); Functions ⤵

  74. function(hello() { console.log("Hello world!"); }); hello(); Call the method named

    “function”… Functions ⤵
  75. function(hello() { console.log("Hello world!"); }); hello(); …with this argument. Functions

  76. function(hello() { console.log("Hello world!"); }); hello(); What is it? Functions

  77. function(hello() { console.log("Hello world!"); }); hello(); Call the method named

    “hello”… Functions ⤵
  78. function(hello() { console.log("Hello world!"); }); hello(); …but what is this?

    Functions ⤵
  79. function(hello() { console.log("Hello world!"); }); hello(); A block argument to

    the “hello” method. Functions ⤵
  80. function(hello() { console.log("Hello world!"); }); hello(); Call the method named

    “hello”. Functions ⤵
  81. function(hello() { console.log("Hello world!"); }); hello(); Undefined methods! Functions ⤵

  82. 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 ⤵
  83. 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! % %
  84. None
  85. Arguments S

  86. 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
  87. 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
  88. None
  89. gem install javascript

  90. chancancode/javascript

  91. None
  92. None
  93. Why? T

  94. class Post < ActiveRecord::Base before_create &javascript { function(post) { console.log(`Created

    ${post.title}`); } } end app/models/post.rb Why? T
  95. Why not? U

  96. Godfrey Chan @chancancode