Slide 1

Slide 1 text

Dropping Down To The Metal™ ™

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Java ☕

Slide 4

Slide 4 text

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! % % %

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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 ')' % %

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

How Ruby Works J hello.rb Output Ruby Interpreter Compile Execute Syntax Error Runtime Error Parse

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

How Ruby Works J hello.rb Output Ruby Interpreter Compile Execute Syntax Error Runtime Error Parse

Slide 13

Slide 13 text

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 ')' % %

Slide 14

Slide 14 text

JavaScript

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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! % %

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

require "javascript" javascript do console.log("Hello world!"); end HelloWorld.java hello.rb JavaScript ☕ Terminal % ruby hello.rb Hello world! % %

Slide 30

Slide 30 text

DSL Domain-specific Language M

Slide 31

Slide 31 text

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 ': undefined local variable or method `console' for main:Object (NameError) % %

Slide 32

Slide 32 text

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! % %

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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 ': undefined local variable or method `console' for main:Object (NameError) % % Instance of Object

Slide 35

Slide 35 text

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 ': undefined local variable or method `console' for main:Object (NameError) % % N self is instance of JavaScript O self is instance of Object

Slide 36

Slide 36 text

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! % %

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

Variables ℹ

Slide 39

Slide 39 text

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 ℹ

Slide 40

Slide 40 text

$ 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 ': undefined method `let' for # (NoMethodError) % %

Slide 41

Slide 41 text

let message = "Hello world!"; Variables ℹ

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

let(message = "Hello world!"); Variables ℹ Call the method named “let”…

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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 ℹ

Slide 52

Slide 52 text

message = "Hello world!"; let("Hello world!"); Variables ℹ $ Q

Slide 53

Slide 53 text

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 ℹ

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

wait a second… J

Slide 56

Slide 56 text

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 ℹ

Slide 57

Slide 57 text

$ 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 ': undefined method `message’ for # (NoMethodError) % %

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

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 ℹ

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

Functions ⤵

Slide 69

Slide 69 text

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 ⤵

Slide 70

Slide 70 text

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 ⤵

Slide 71

Slide 71 text

$ It works™ ) Syntax Error - No Method Error ( Name Error console.log("Hello world!"); hello.rb Terminal % ruby hello.rb % % * None of the above Functions ⤵

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

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

Slide 80

Slide 80 text

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

Slide 81

Slide 81 text

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

Slide 82

Slide 82 text

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 ⤵

Slide 83

Slide 83 text

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! % %

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

Arguments S

Slide 86

Slide 86 text

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

Slide 87

Slide 87 text

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

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

gem install javascript

Slide 90

Slide 90 text

chancancode/javascript

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

Why? T

Slide 94

Slide 94 text

class Post < ActiveRecord::Base before_create &javascript { function(post) { console.log(`Created ${post.title}`); } } end app/models/post.rb Why? T

Slide 95

Slide 95 text

Why not? U

Slide 96

Slide 96 text

Godfrey Chan @chancancode