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

Dropping down to The Metal™

Godfrey Chan
June 20, 2015
230

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!

Godfrey Chan

June 20, 2015
Tweet

Transcript

  1. 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 ®
  2. Programmer to Thought Leader™ Professional Thought Leadership Aaron Patterson ,

    Gorby Puff, David Heinemeier Hansson g g n n o o r r w w ™
  3. function square(x) { return (x * x); } function diag(a,

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

    b) { return Math.sqrt( square(a) + square(b) ); } JIT Hints
  5. 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
  6. 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
  7. 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
  8. +

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

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

    void main(String[] args) { System.out.println("Hello, World"); } } java.rb % ruby java.rb ??? Terminal
  11. 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™
  12. 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
  13. 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
  14. 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
  15. Running JS with Ruby console.log("Hello world!"); javascript.rb % ruby javascript.rb

    ??? Terminal A. It works™ B. Syntax Error C. Runtime Error
  16. 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
  17. Running JS with Ruby % ruby javascript.rb NameError: undefined local

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

    end console = Console.new # Begin "JavaScript" console.log("Hello world!"); javascript.rb
  19. 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
  20. 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
  21. 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 ಠ_ಠ
  22. Running JS with Ruby # Magic here... javascript.rb require 'javascript'

    javascript do console.log("Hello world!"); end hello_world.rb
  23. 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
  24. Variables require 'javascript' javascript do var message = "Hello world!";

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

    console.log(message); end hello_world.rb % ruby hello_world.rb ??? Terminal A. It works™
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. Uninitialized Variables javascript do var message; message = "Hello world!";

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

    console.log(message); end hello_world.rb A. It works™ % ruby hello_world.rb ??? Terminal
  33. 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
  34. 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
  35. 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
  36. 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
  37. Uninitialized Variables % ruby hello_world.rb NameError: undefined local variable or

    method `message' for #<JavaScript::Contex " from hello_world.rb:2 Terminal
  38. 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
  39. Functions javascript do function hello() { console.log("Hello world!"); } hello();

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

    end hello_world.rb A. It works™ % ruby hello_world.rb ??? Terminal
  41. 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
  42. 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
  43. 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
  44. 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
  45. 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
  46. 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
  47. 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() { ... }
  48. 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
  49. 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
  50. 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
  51. 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
  52. 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
  53. Function Arguments javascript do function hello(name) { console.log("Hello " +

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

    name + "!"); } hello("world"); end hello_world.rb % ruby hello_world.rb ??? Terminal
  55. 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
  56. 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
  57. 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
  58. 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
  59. 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
  60. 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
  61. 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
  62. 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