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

Dropping down to The Metal™

22bb3e56828870ee9a0dd93aeadbe04a?s=47 Godfrey Chan
June 20, 2015
200

Dropping down to The Metal™

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/vanruby/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

June 20, 2015
Tweet

Transcript

  1. Godfrey Chan @chancancode

  2. None
  3. None
  4. eh? Accent

  5. eh? Accent Healthcare

  6. eh? Accent Healthcare Texas

  7. eh? Accent Healthcare $ Taxes

  8. eh? Accent gem install canada

  9. eh? Accent >> require 'canada' => true >> [].empty_eh? =>

    true >> [1,2,3].empty_eh? => false
  10. eh? Accent Healthcare $ Taxes

  11. None
  12. None
  13. Impress your peers with powerful thought leadership skills! Thought Leadership

    A Reference for the Rest of Us!® FREE eTips at dummies.com® All the best kept secrets of seasoned thought leaders in one book ®
  14. Programmer to Thought Leader™ Professional Thought Leadership Aaron Patterson ,

    Gorby Puff, David Heinemeier Hansson g g n n o o r r w w ™
  15. Chapter 1 Create Controversies “TDD is dead.” David Heinemeier Hansson

  16. None
  17. Thought Leadership Is Dead

  18. http://rails-weekly.goodbits.io

  19. Dropping Down To The Metal™ ™

  20. JavaScript

  21. None
  22. Syntax

  23. JIT Hints

  24. JIT Hints

  25. JIT Hints

  26. JIT Hints

  27. function square(x) { return (x * x); } function diag(a,

    b) { return Math.sqrt( square(a) + square(b) ); } JIT Hints
  28. function square(x) { return (x * x); } function diag(a,

    b) { return Math.sqrt( square(a) + square(b) ); } JIT Hints
  29. function square(x) { x = +x; return +(x * x);

    } function diag(a, b) { a = +a; b = +b; return +Math.sqrt(square(a) + square(b)); } JIT Hints
  30. function square(x) { x = +x; return +(x * x);

    } function diag(a, b) { a = +a; b = +b; return +Math.sqrt(square(a) + square(b)); } x = +x; + a = +a; b = +b; + JIT Hints
  31. function square(x) { x = +x; return +(x * x);

    } function diag(a, b) { a = +a; b = +b; return +Math.sqrt(square(a) + square(b)); } x = +x; + a = +a; b = +b; + Much Speed 5% Faster WOW Seems le JIT JIT Hints
  32. 660M 673M 685M 698M 710M Ops/sec Such Chart w/o JIT

    Hints w/ JIT Hints JIT Hints
  33. 660M 673M 685M 698M 710M Ops/sec Such Chart w/o JIT

    Hints w/ JIT Hints JIT Hints
  34. 0M 178M 355M 533M 710M Ops/sec Such Chart w/o JIT

    Hints w/ JIT Hints JIT Hints
  35. +

  36. JS Syntax +

  37. JS Syntax Speed +

  38. JS Syntax Speed Ruby VM +

  39. JS Syntax Speed Ruby VM Magic +

  40. JS Syntax Speed Ruby VM Magic + Awesome

  41. JS Syntax Speed Ruby VM Magic + Awesome Amaze

  42. None
  43. gem install javascript

  44. Java

  45. Running Java with Ruby public class HelloWorld { public static

    void main(String[] args) { System.out.println("Hello, World"); } } java.rb
  46. Running Java with Ruby public class HelloWorld { public static

    void main(String[] args) { System.out.println("Hello, World"); } } java.rb % ruby java.rb ??? Terminal
  47. Running Java with Ruby public class HelloWorld { public static

    void main(String[] args) { System.out.println("Hello, World"); } } java.rb % ruby java.rb ??? Terminal A. It works™
  48. Running Java with Ruby public class HelloWorld { public static

    void main(String[] args) { System.out.println("Hello, World"); } } java.rb % ruby java.rb ??? Terminal A. It works™ B. Syntax Error
  49. Running Java with Ruby public class HelloWorld { public static

    void main(String[] args) { System.out.println("Hello, World"); } } java.rb % ruby java.rb ??? Terminal A. It works™ B. Syntax Error C. Runtime Error
  50. Running Java with Ruby A. It works™ B. Syntax Error

    C. Runtime Error public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } } java.rb % ruby java.rb java.rb:2: syntax error, unexpected tIDENTIFIER, expecting ')' public static void main(String[] args) { ^ java.rb:4: syntax error, unexpected '}', expecting '(' Terminal
  51. How Ruby works Your Code Output Ruby Interpreter ???

  52. How Ruby works Your Code Output Ruby Interpreter Parse Evaluate

  53. How Ruby works Your Code Output Ruby Interpreter Parse Evaluate

    Syntax Error
  54. How Ruby works Your Code Output Ruby Interpreter Parse Evaluate

    Syntax Error Runtime Error
  55. JavaScript

  56. Running JS with Ruby console.log("Hello world!"); javascript.rb

  57. Running JS with Ruby console.log("Hello world!"); javascript.rb % ruby javascript.rb

    ??? Terminal
  58. Running JS with Ruby console.log("Hello world!"); javascript.rb % ruby javascript.rb

    ??? Terminal A. It works™
  59. Running JS with Ruby console.log("Hello world!"); javascript.rb % ruby javascript.rb

    ??? Terminal A. It works™ B. Syntax Error
  60. Running JS with Ruby console.log("Hello world!"); javascript.rb % ruby javascript.rb

    ??? Terminal A. It works™ B. Syntax Error C. Runtime Error
  61. console.log("Hello world!"); javascript.rb Running JS with Ruby A. It works™

    B. Syntax Error C. Runtime Error % ruby javascript.rb NameError: undefined local variable or method `console' for main:Object " from javascript.rb:1 Terminal
  62. Running JS with Ruby console.log("Hello world!");

  63. Running JS with Ruby console.log("Hello world!"); Local variable (or method)

    named “console”
  64. Running JS with Ruby console.log("Hello world!"); Call instance method “log”

    on “console”
  65. Running JS with Ruby console.log("Hello world!"); Optional parenthesis

  66. Running JS with Ruby console.log("Hello world!"); Method argument (String literal

    “Hello world!”)
  67. Running JS with Ruby console.log("Hello world!"); Optional semicolon

  68. Running JS with Ruby % ruby javascript.rb NameError: undefined local

    variable or method `console' for main:Object " from javascript.rb:1 Terminal
  69. Running JS with Ruby class Console def log(*args) puts(*args) end

    end console = Console.new # Begin "JavaScript" console.log("Hello world!"); javascript.rb
  70. Running JS with Ruby class Console def log(*args) puts(*args) end

    end console = Console.new # Begin "JavaScript" console.log("Hello world!"); javascript.rb % ruby javascript.rb Hello world! % Terminal
  71. None
  72. Running JS with Ruby class Console def log(*args) puts(*args) end

    end console = Console.new # Begin "JavaScript" console.log("Hello world!"); javascript.rb % ruby javascript.rb Hello world! % Terminal
  73. Running JS with Ruby class Console def log(*args) puts(*args) end

    end console = Console.new # Begin "JavaScript" console.log("Hello world!"); javascript.rb % ruby javascript.rb Hello world! % Terminal ಠ_ಠ
  74. Running JS with Ruby # Magic here... javascript.rb require 'javascript'

    javascript do console.log("Hello world!"); end hello_world.rb
  75. DSLs

  76. instance_exec

  77. instance_exec module JavaScript class Console; ... ; end class Context

    def console Console.new end end end module Kernel def javascript(&block) JavaScript::Context.new.instance_exec(&block) end end javascript.rb require 'javascript' javascript do console.log("Hello world!"); end hello_world.rb
  78. None
  79. Variables

  80. Variables require 'javascript' javascript do var message = "Hello world!";

    console.log(message); end hello_world.rb
  81. Variables require 'javascript' javascript do var message = "Hello world!";

    console.log(message); end hello_world.rb % ruby hello_world.rb ??? Terminal
  82. Variables require 'javascript' javascript do var message = "Hello world!";

    console.log(message); end hello_world.rb % ruby hello_world.rb ??? Terminal A. It works™
  83. Variables require 'javascript' javascript do var message = "Hello world!";

    console.log(message); end hello_world.rb % ruby hello_world.rb ??? Terminal A. It works™ B. Syntax Error
  84. Variables require 'javascript' javascript do var message = "Hello world!";

    console.log(message); end hello_world.rb % ruby hello_world.rb ??? Terminal A. It works™ B. Syntax Error C. NoMethodError
  85. Variables require 'javascript' javascript do var message = "Hello world!";

    console.log(message); end hello_world.rb % ruby hello_world.rb ??? Terminal A. It works™ B. Syntax Error C. NoMethodError D. NameError
  86. require 'javascript' javascript do var message = "Hello world!"; console.log(message);

    end hello_world.rb Variables A. It works™ B. Syntax Error C. NoMethodError D. NameError % ruby hello_world.rb NoMethodError: undefined method `var' for #<JavaScript::Context:...> " from hello_world.rb:5 Terminal
  87. Variables var message = "Hello world!";

  88. Variables var(message = "Hello world!");

  89. Variables var(message = "Hello world!"); Optional semicolon

  90. Variables var(message = "Hello world!"); Optional parenthesis

  91. Variables var(message = "Hello world!"); String literal “Hello world!”

  92. Variables var(message = "Hello world!"); Variable assignment

  93. Variables var(message = "Hello world!"); Local variable “message”

  94. Variables var(message = "Hello world!"); Call method named “var”

  95. Variables var(message = "Hello world!"); Variable assignment “returns” the assigned

    value!
  96. Variables message = "Hello world!"; var(message);

  97. Variables % ruby hello_world.rb NoMethodError: undefined method `var' for #<JavaScript::Context:...>

    " from hello_world.rb:5 Terminal
  98. Variables module JavaScript ... class Context ... def var(*) end

    end end ... javascript.rb require 'javascript' javascript do var message = "Hello world!"; console.log(message); end hello_world.rb
  99. None
  100. Uninitialized Variables

  101. Uninitialized Variables javascript do var message; message = "Hello world!";

    console.log(message); end hello_world.rb
  102. Uninitialized Variables javascript do var message; message = "Hello world!";

    console.log(message); end hello_world.rb % ruby hello_world.rb ??? Terminal
  103. Uninitialized Variables javascript do var message; message = "Hello world!";

    console.log(message); end hello_world.rb A. It works™ % ruby hello_world.rb ??? Terminal
  104. Uninitialized Variables javascript do var message; message = "Hello world!";

    console.log(message); end hello_world.rb A. It works™ B. Syntax Error % ruby hello_world.rb ??? Terminal
  105. Uninitialized Variables javascript do var message; message = "Hello world!";

    console.log(message); end hello_world.rb A. It works™ B. Syntax Error C. NoMethodError % ruby hello_world.rb ??? Terminal
  106. Uninitialized Variables javascript do var message; message = "Hello world!";

    console.log(message); end hello_world.rb A. It works™ B. Syntax Error C. NoMethodError D. NameError % ruby hello_world.rb ??? Terminal
  107. javascript do var message; message = "Hello world!"; console.log(message); end

    hello_world.rb Uninitialized Variables A. It works™ B. Syntax Error C. NoMethodError D. NameError % ruby hello_world.rb NameError: undefined local variable or method `message' for " from hello_world.rb:2 Terminal
  108. Uninitialized Variables var message; message = "Hello world!";

  109. Uninitialized Variables var message(); message = "Hello world!";

  110. Uninitialized Variables var(message()); message = "Hello world!";

  111. Uninitialized Variables % ruby hello_world.rb NameError: undefined local variable or

    method `message' for #<JavaScript::Contex " from hello_world.rb:2 Terminal
  112. Uninitialized Variables module JavaScript ... class Context ... def method_missing(*)

    end end end ... javascript.rb javascript do var message; message = "Hello world!"; console.log(message); end hello_world.rb
  113. None
  114. Functions

  115. Functions javascript do function hello() { console.log("Hello world!"); } hello();

    end hello_world.rb
  116. Functions javascript do function hello() { console.log("Hello world!"); } hello();

    end hello_world.rb % ruby hello_world.rb ??? Terminal
  117. Functions javascript do function hello() { console.log("Hello world!"); } hello();

    end hello_world.rb A. It works™ % ruby hello_world.rb ??? Terminal
  118. Functions javascript do function hello() { console.log("Hello world!"); } hello();

    end hello_world.rb A. It works™ B. Syntax Error % ruby hello_world.rb ??? Terminal
  119. Functions javascript do function hello() { console.log("Hello world!"); } hello();

    end hello_world.rb A. It works™ B. Syntax Error C. NoMethodError % ruby hello_world.rb ??? Terminal
  120. Functions javascript do function hello() { console.log("Hello world!"); } hello();

    end hello_world.rb A. It works™ B. Syntax Error C. NoMethodError D. NameError % ruby hello_world.rb ??? Terminal
  121. Functions javascript do function hello() { console.log("Hello world!"); } hello();

    end hello_world.rb A. It works™ B. Syntax Error C. NoMethodError D. NameError % ruby hello_world.rb ??? Terminal
  122. Functions javascript do function hello() { console.log("Hello world!"); } hello();

    end hello_world.rb A. It works™ B. Syntax Error C. NoMethodError D. NameError E. Nothing Happens % ruby hello_world.rb % Terminal
  123. Functions function hello() { console.log("Hello world!"); }

  124. Functions function hello() { console.log("Hello world!"); } Invoke method “hello”

  125. Functions With no “regular” arguments function hello() { console.log("Hello world!");

    }
  126. Functions ...and a block argument function hello() { console.log("Hello world!");

    }
  127. Functions ...then invoke the method “function” with its result function

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

  129. Functions temp = hello { console.log("Hello world") } function(temp)

  130. temp = hello { console.log("Hello world") } function(temp) Functions Undefined

    methods!
  131. Functions temp = hello { console.log("Hello world") } function(temp) ???!

  132. Functions module JavaScript class Context def method_missing(*) end end end

    javascript.rb
  133. Functions module JavaScript class Context def function(*) end def method_missing(name,

    *, &block) if block define_singleton_method(name, block) end end end end javascript.rb javascript do function hello() { ... } hello(); end hello_world.rb
  134. javascript.rb module JavaScript class Context def function(*) end def method_missing(name,

    *, &block) if block define_singleton_method(name, block) end end end end Functions def method_missing(name, *, &block) end hello_world.rb javascript do function hello() { ... } hello(); end hello() { ... }
  135. javascript.rb Functions def method_missing(name, *, &block) if block end hello_world.rb

    javascript do function hello() { ... } hello(); end hello() { ... } module JavaScript class Context def function(*) end def method_missing(name, *, &block) if block define_singleton_method(name, block) end end end end
  136. javascript.rb Functions def method_missing(name, *, &block) if block define_singleton_method(name, block)

    end hello_world.rb javascript do function hello() { ... } hello(); end hello() { ... } module JavaScript class Context def function(*) end def method_missing(name, *, &block) if block define_singleton_method(name, block) end end end end
  137. javascript.rb Functions hello_world.rb javascript do function hello() { ... }

    hello(); end function hello() { ... } module JavaScript class Context def function(*) end def method_missing(name, *, &block) if block define_singleton_method(name, block) end end end end
  138. javascript.rb Functions hello_world.rb javascript do function hello() { ... }

    hello(); end function hello() { ... } module JavaScript class Context def function(*) end def method_missing(name, *, &block) if block define_singleton_method(name, block) end end end end def function(*) end
  139. javascript.rb Functions hello_world.rb javascript do function hello() { ... }

    hello(); end hello(); module JavaScript class Context def function(*) end def method_missing(name, *, &block) if block define_singleton_method(name, block) end end end end
  140. None
  141. Function Arguments

  142. Function Arguments javascript do function hello(name) { console.log("Hello " +

    name + "!"); } hello("world"); end hello_world.rb
  143. Function Arguments javascript do function hello(name) { console.log("Hello " +

    name + "!"); } hello("world"); end hello_world.rb % ruby hello_world.rb ??? Terminal
  144. Function Arguments javascript do function hello(name) { console.log("Hello " +

    name + "!"); } hello("world"); end hello_world.rb A. It works™ % ruby hello_world.rb ??? Terminal
  145. Function Arguments javascript do function hello(name) { console.log("Hello " +

    name + "!"); } hello("world"); end hello_world.rb A. It works™ B. Syntax Error % ruby hello_world.rb ??? Terminal
  146. Function Arguments javascript do function hello(name) { console.log("Hello " +

    name + "!"); } hello("world"); end hello_world.rb A. It works™ B. Syntax Error C. ArgumentError % ruby hello_world.rb ??? Terminal
  147. Function Arguments javascript do function hello(name) { console.log("Hello " +

    name + "!"); } hello("world"); end hello_world.rb A. It works™ B. Syntax Error C. ArgumentError D. NameError % ruby hello_world.rb ??? Terminal
  148. Function Arguments javascript do function hello(name) { console.log("Hello " +

    name + "!"); } hello("world"); end hello_world.rb A. It works™ B. Syntax Error C. ArgumentError D. NameError E. Nothing Happens % ruby hello_world.rb ??? Terminal
  149. Function Arguments javascript do function hello(name) { console.log("Hello " +

    name + "!"); } hello("world"); end hello_world.rb A. It works™ B. Syntax Error C. ArgumentError D. NameError E. Nothing Happens % ruby hello_world.rb ??? Terminal
  150. Function Arguments javascript do function hello(name) { console.log("Hello " +

    name + "!"); } hello("world"); end hello_world.rb A. It works™ B. Syntax Error C. ArgumentError D. NameError E. Nothing Happens % ruby hello_world.rb ??? Terminal
  151. None
  152. vanruby/javascript

  153. Such tests! test "Functions can return a value" do require

    "javascript" javascript { function identity(x) { return x; } function square(x) { return x * x; } console.log(identity("Hello world!")); console.log(square(2)); } assert_messages "Hello world!", 4 end
  154. ಠ_ಠ

  155. Why?

  156. ¯\_(π)_/¯

  157. Why not?

  158. @chancancode