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!

June 20, 2015

## Transcript

2. None
3. None

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

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

11. None
12. None

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 ™

16. None

21. None

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

42. None

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

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

Syntax Error Runtime Error

??? 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

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

named “console”

on “console”

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

“Hello world!”)

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

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

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

value!

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

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

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

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

}

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

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

methods!

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

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

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