jruby @ boulder ruby

62
Nick Sieger @nicksieger

Upload: nick-sieger

Post on 15-May-2015

637 views

Category:

Technology


2 download

DESCRIPTION

Re-introduction to JRuby as presented to the Boulder Ruby group on October 30, 2012.

TRANSCRIPT

Page 1: JRuby @ Boulder Ruby

Nick Sieger@nicksieger

Page 2: JRuby @ Boulder Ruby
Page 3: JRuby @ Boulder Ruby

(RE-)INTRO

Page 4: JRuby @ Boulder Ruby

Ruby on the JVM

Page 5: JRuby @ Boulder Ruby

Best of Ruby and Java...Together!

Page 7: JRuby @ Boulder Ruby

COMPATIBILITY

Page 8: JRuby @ Boulder Ruby

runs nearly all pure ruby code

runs 92% of rubyspec suite

runs ~30% of 1.9 test suite

(even more with 1.8 mode)

Page 9: JRuby @ Boulder Ruby

INSTALL gem install jruby-lint

http://jruby.org/download

rvm install jruby

$ jrlint

Page 10: JRuby @ Boulder Ruby

drawbacks

Page 11: JRuby @ Boulder Ruby

startup time

memory footprint

native code

no Kernel#fork

Page 12: JRuby @ Boulder Ruby

PERFORMANCE

Page 13: JRuby @ Boulder Ruby

GC.pauses = “no thanks”

Page 14: JRuby @ Boulder Ruby

class Simple  attr_accessor :nextend

top = Simple.new

puts Benchmark.measure {  outer = 10  total = 100000  per = 100

  outer.times do    total.times do      per.times { Simple.new }      s = Simple.new      top.next = s      top = s    end  end}

Page 15: JRuby @ Boulder Ruby

0

2000

4000

6000

8000

GC count

Ruby 1.9.3 JRuby

Page 16: JRuby @ Boulder Ruby

1

10

100

1000

10000

GC count

Ruby 1.9.3 JRuby

Page 17: JRuby @ Boulder Ruby

0

3

6

9

12

15

GC time %

Ruby 1.9.3 JRuby

Page 18: JRuby @ Boulder Ruby

0ms

75ms

150ms

225ms

300ms

188KB/29MB 27MB/127MB 199MB/238MB

Time per GC versus heap usage

Heap usage (MRI/JRuby)

Ruby 2.0.0 JRuby

Page 19: JRuby @ Boulder Ruby

REAL THREADS

Page 20: JRuby @ Boulder Ruby

Ruby 1.9unthreaded

Ruby 1.9threaded

JRubyunthreaded

JRubythreaded

Page 21: JRuby @ Boulder Ruby

sidekiq.orgWhat if 1 Sidekiq process could do the work of 20 Resque or DelayedJob processes?

Page 22: JRuby @ Boulder Ruby

COMPILER

Page 23: JRuby @ Boulder Ruby

Native code

JVM bytecode

Ruby application code

JVM

JRuby

Ruby code

JVM

JRuby

Ruby code

JRuby

Ruby code

JVM

JRuby

Ruby code

JRuby

Ruby code

Ruby code

Full interpretation JRuby compiler HotSpot compiler

Time

Key

Page 24: JRuby @ Boulder Ruby

BENCH MARKS

Page 25: JRuby @ Boulder Ruby

0

1

2

3

4

5

Red-Black Tree

ruby 1.9.3p286

jruby 1.7.0 java 1.6.0_29

jruby 1.7.0 java 1.7.0_09

jruby 1.7.0 java 1.7.0_09-b05 +indy

Page 26: JRuby @ Boulder Ruby

0

1.25

2.5

3.75

5

base64 richards neural mandelbrot redblack

4.324.226

3.663.44

2.658

1.5651.8061.914

1.5381.346

Times Faster than Ruby 1.9.3

JRuby/Java 6 JRuby/Java 7+indy

Page 27: JRuby @ Boulder Ruby

Java 7

Page 28: JRuby @ Boulder Ruby

Get JDK 7(OSX too!)

java.oracle.com

Page 29: JRuby @ Boulder Ruby

INVOKEDYNAMIC

Page 30: JRuby @ Boulder Ruby

experthuman.com/programming-with-

nothingTom Stuart@tomstuart

Page 31: JRuby @ Boulder Ruby

# lambda-> x {}

Page 32: JRuby @ Boulder Ruby

# callp[x]

Page 33: JRuby @ Boulder Ruby

FIZZ BUZZ

Page 34: JRuby @ Boulder Ruby

(1..100).each do |n| if (n % 15).zero? puts 'FizzBuzz' elsif (n % 3).zero? puts 'Fizz' elsif (n % 5).zero? puts 'Buzz' else puts n.to_s endend

Page 35: JRuby @ Boulder Ruby

fizzbuzz = -> k { -> f { -> f { -> x { f[-> y { x[x][y] }] }[-> x {f[-> y { x[x][y] }] }] }[-> f { -> l { -> x { -> g { -> b { b }[-> p {p[-> x { -> y { x } } ] }[l]][x][-> y { g[f[-> l { -> p { p[-> x { ->y { y } } ] }[-> p { p[-> x { -> y { y } } ] }[l]] }[l]][x][g]][-> l {-> p { p[-> x { -> y { x } } ] }[-> p { p[-> x { -> y { y } } ] }[l]]}[l]][y] }] } } } }][k][-> x { -> y { -> f { f[x][y] } } }[-> x { -> y{ x } }][-> x { -> y { x } }]][-> l { -> x { -> l { -> x { -> x { -> y{ -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f {f[x][y] } } }[x][l]] } }[l][f[x]] } }] } }[-> f { -> x { f[-> y {x[x][y] }] }[-> x { f[-> y { x[x][y] }] }] }[-> f { -> m { -> n { -> b{ b }[-> m { -> n { -> n { n[-> x { -> x { -> y { y } } }][-> x { -> y{ x } }] }[-> m { -> n { n[-> n { -> f { -> x { n[-> g { -> h {h[g[f]] } }][-> y { x }][-> y { y }] } } }][m] } }[m][n]] }}[m][n]][-> x { -> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x{ -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[f[-> n {-> p { -> x { p[n[p][x]] } } }[m]][n]][m][x] }][-> x { -> y { -> f {f[x][y] } } }[-> x { -> y { x } }][-> x { -> y { x } }]] } } }][-> p {-> x { p[x] } }][-> p { -> x {p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[x]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]} }]][-> n { -> b {b }[-> n { n[-> x { -> x { -> y { y } } }][-> x { -> y { x } }] }[-> f{ -> x { f[-> y { x[x][y] }] }[-> x { f[-> y { x[x][y] }] }] }[-> f {-> m { -> n { -> b { b }[-> m { -> n { -> n { n[-> x { -> x { -> y { y} } }][-> x { -> y { x } }] }[-> m { -> n { n[-> n { -> f { -> x {n[-> g { -> h { h[g[f]] } }][-> y { x }][-> y { y }] } } }][m] }}[m][n]] } }[n][m]][-> x { f[-> m { -> n { n[-> n { -> f { -> x { n[->g { -> h { h[g[f]] } }][-> y { x }][-> y { y }] } } }][m] }}[m][n]][n][x] } ][m] } } }][n][-> p { -> x {p[p[p[p[p[p[p[p[p[p[p[p[p[p[p[x]]]]]]]]]]]]]]] } }]]][-> l { -> x { ->x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { ->f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f {f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } }}[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x {-> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { ->x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { ->y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f {f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } }}[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x {-> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { ->x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { ->y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f {f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } }}[x][l]] } }[-> x { -> y { -> f { f[x][y] } } }[-> x { -> y { x }}][-> x { -> y { x } }]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n {-> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } }}[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n {n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x }}] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] }}]]]]]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x {p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p {-> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p {-> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { ->x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]]]][-> n { -> p{ -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n {-> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n {-> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[->p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]]][-> m{ -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] }}[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x{ p[p[p[p[p[x]]]]] } }]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n {-> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } }}[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n {n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x }}] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] }}]]]]]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x {p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p {-> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p {-> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { ->x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]]]][-> n { -> p{ -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m {-> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] }}[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x{ p[p[p[p[p[x]]]]] } }]]]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> m{ -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] }}[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x{ p[p[p[p[p[x]]]]] } }]]]][-> b { b }[-> n { n[-> x { -> x { -> y { y} } }][-> x { -> y { x } }] }[-> f { -> x { f[-> y { x[x][y] }] }[-> x{ f[-> y { x[x][y] }] }] }[-> f { -> m { -> n { -> b { b }[-> m { -> n{ -> n { n[-> x { -> x { -> y { y } } }][-> x { -> y { x } }] }[-> m {-> n { n[-> n { -> f { -> x { n[-> g { -> h { h[g[f]] } }][-> y { x}][-> y { y }] } } }][m] } }[m][n]] } }[n][m]][-> x { f[-> m { -> n {n[-> n { -> f { -> x { n[-> g { -> h { h[g[f]] } }][-> y { x }][-> y {y }] } } }][m] } }[m][n]][n][x] }][m] } } }][n][-> p { -> x {p[p[p[x]]] } }]]][-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[->x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l {-> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x {-> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { ->f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] }} }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x {-> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> x { ->y { -> f { f[x][y] } } }[-> x { -> y { x } }][-> x { -> y { x } }]][->n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } }}[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x {p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x {p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x {p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]]]]]][-> n { -> p {-> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n {-> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } }}[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } }}][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p{ -> x { p[p[p[p[p[x]]]]] } }]]]]]]][-> n { -> p { -> x { p[n[p][x]] }} }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { -> n{ n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x} }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] }}]]]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n { n[-> m { ->n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x {x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] }}]]]][-> b { b }[-> n { n[-> x { -> x { -> y { y } } }][-> x { -> y {x } }] }[-> f { -> x { f[-> y { x[x][y] }] }[-> x { f[-> y { x[x][y]}] }] }[-> f { -> m { -> n { -> b { b }[-> m { -> n { -> n { n[-> x {-> x { -> y { y } } }][-> x { -> y { x } }] }[-> m { -> n { n[-> n {-> f { -> x { n[-> g { -> h { h[g[f]] } }][-> y { x }][-> y { y }] } }}][m] } }[m][n]] } }[n][m]][-> x { f[-> m { -> n { n[-> n { -> f { ->x { n[-> g { -> h { h[g[f]] } }][-> y { x }][-> y { y }] } } }][m] }}[m][n]][n][x] }][m] } } }][n][-> p { -> x { p[p[p[p[p[x]]]]] }}]]][-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y} }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> l { -> x { -> x{ -> y { -> f { f[x][y] } } }[-> x { -> y { y } }][-> x { -> y { -> f{ f[x][y] } } }[x][l]] } }[-> l { -> x { -> x { -> y { -> f { f[x][y]} } }[-> x { -> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] }}[-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y { y }}][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> x { -> y { -> f {f[x][y] } } }[-> x { -> y { x } }][-> x { -> y { x } }]][-> n { -> p {-> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n {-> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } }}[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } }}][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p{ -> x { p[p[p[p[p[x]]]]] } }]]]]]]][-> n { -> p { -> x { p[n[p][x]] }} }[-> n { -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x {p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } } }[-> m { -> n {n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][->p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x {p[p[p[p[p[x]]]]] } }]]]]]]][-> n { -> p { -> x { p[n[p][x]] } } }[-> n{ -> p { -> x { p[n[p][x]] } } }[-> n { -> p { -> x { p[n[p][x]] } }}[-> m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } }}][m] } }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p{ -> x { p[p[p[p[p[x]]]]] } }]]]]]][-> m { -> n { n[-> m { -> n { n[->n { -> p { -> x { p[n[p][x]] } } }][m] } }[m]][-> p { -> x { x } }] }}[-> p { -> x { p[p[x]] } }][-> p { -> x { p[p[p[p[p[x]]]]] } }]]][->f { -> x { f[-> y { x[x][y] }] }[-> x { f[-> y { x[x][y] }] }] }[-> f{ -> n { -> l { -> x { -> f { -> x { f[-> y { x[x][y] }] }[-> x { f[->y { x[x][y] }] }] }[-> f { -> l { -> x { -> g { -> b { b }[-> p { p[->x { -> y { x } }] }[l]][x][-> y { g[f[-> l { -> p { p[-> x { -> y { y} }] }[-> p { p[-> x { -> y { y } }] }[l]] }[l]][x][g]][-> l { -> p {p[-> x { -> y { x } }] }[-> p { p[-> x { -> y { y } }] }[l]] }[l]][y]}] } } } }][l][-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x {-> y { y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }[-> x { ->y { -> f { f[x][y] } } }[-> x { -> y { x } }][-> x { -> y { x }}]][x]][-> l { -> x { -> x { -> y { -> f { f[x][y] } } }[-> x { -> y {y } }][-> x { -> y { -> f { f[x][y] } } }[x][l]] } }] } }[-> b { b}[-> m { -> n { -> n { n[-> x { -> x { -> y { y } } }][-> x { -> y { x} }] }[-> m { -> n { n[-> n { -> f { -> x { n[-> g { -> h { h[g[f]] }}][-> y { x }][-> y { y }] } } }][m] } }[m][n]] } }[n][-> n { -> f {-> x { n[-> g { -> h { h[g[f]] } }][-> y { x }][-> y { y }] } } }[-> m{ -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] }}[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x{ p[p[p[p[p[x]]]]] } }]]]][-> x { -> y { -> f { f[x][y] } } }[-> x {-> y { x } }][-> x { -> y { x } }]][-> x { f[-> f { -> x { f[-> y {x[x][y] }] }[-> x { f[-> y { x[x][y] }] }] }[-> f { -> m { -> n { -> b{ b }[-> m { -> n { -> n { n[-> x { -> x { -> y { y } } }][-> x { -> y{ x } }] }[-> m { -> n { n[-> n { -> f { -> x { n[-> g { -> h {h[g[f]] } }][-> y { x }][-> y { y }] } } }][m] } }[m][n]] }}[n][m]][-> x { -> n { -> p { -> x { p[n[p][x]] } } }[f[-> m { -> n {n[-> n { -> f { -> x { n[-> g { -> h { h[g[f]] } }][-> y { x }][-> y {y }] } } }][m] } }[m][n]][n]][x] }][-> p { -> x { x } }] } } }][n][->m { -> n { n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m]} }[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { ->x { p[p[p[p[p[x]]]]] } }]]][x] }]][-> f { -> x { f[-> y { x[x][y] }]}[-> x { f[-> y { x[x][y] }] }] }[-> f { -> m { -> n { -> b { b }[-> m{ -> n { -> n { n[-> x { -> x { -> y { y } } }][-> x { -> y { x } }]}[-> m { -> n { n[-> n { -> f { -> x { n[-> g { -> h { h[g[f]] } }][->y { x }][-> y { y }] } } }][m] } }[m][n]] } }[n][m]][-> x { f[-> m {-> n { n[-> n { -> f { -> x { n[-> g { -> h { h[g[f]] } }][-> y { x}][-> y { y }] } } }][m] } }[m][n]][n][x] }][m] } } }][n][-> m { -> n{ n[-> m { -> n { n[-> n { -> p { -> x { p[n[p][x]] } } }][m] }}[m]][-> p { -> x { x } }] } }[-> p { -> x { p[p[x]] } }][-> p { -> x{ p[p[p[p[p[x]]]]] } }]]] } }][n]]]] }]

Page 36: JRuby @ Boulder Ruby

METHOD HANDLES

Page 37: JRuby @ Boulder Ruby

INVOKEDYNAMIC[bootstrapMethod]

bootstrapMethod():CallSite (once)

Call Site

MethodHandle

target

Page 38: JRuby @ Boulder Ruby

package java.lang.invoke;

public abstract class MethodHandle { public final native Object invoke(Object... args) throws Throwable;}

Page 39: JRuby @ Boulder Ruby

LAMBDAp[x]

Page 40: JRuby @ Boulder Ruby

methodHandle.invoke(x);

Page 41: JRuby @ Boulder Ruby

arrayElementGetter arrayElementSetter spreadInvoker exactInvoker invoker

explicitCastArguments permuteArguments constant identity insertArguments

dropArguments dropArguments filterArguments filterReturnValue

foldArguments guardWithTest catchException throwException

java.lang.invoke.MethodHandles

Page 42: JRuby @ Boulder Ruby

String literal

Other literals

Constant lookup

Instance variables

Method invocation

Math operations

Page 43: JRuby @ Boulder Ruby

IT’S A JVM WORLD

Page 44: JRuby @ Boulder Ruby

DRIVE JAVA FROM RUBY

Page 45: JRuby @ Boulder Ruby

jruby -S gem install flying_saucer to try this example.

require 'java'require 'rubygems'require 'flying_saucer'

java_import org.xhtmlrenderer.pdf.ITextRenderer

document = <<-HTML<html><body><h1>Hello Flying Saucer!</h1></body></html>HTML

File.open("doc.pdf", "wb") do |out| renderer = ITextRenderer.new renderer.set_document_from_string document renderer.layout renderer.create_pdf out.to_outputstreamend

system("open doc.pdf")

Page 46: JRuby @ Boulder Ruby

$ jruby saucer.rb

Page 47: JRuby @ Boulder Ruby

DECORATE JAVA OBJECTS WITH JRUBY

Page 48: JRuby @ Boulder Ruby

module java::util::Iterator include Enumerable

def each while has_next yield self.next end endend

Page 49: JRuby @ Boulder Ruby

DRIVE RUBY FROM JAVA

(org.jruby.embed)

Page 50: JRuby @ Boulder Ruby

import org.jruby.embed.ScriptingContainer;

public class EmbedJRuby { public static void main(String[] args) { ScriptingContainer container = new ScriptingContainer(); container.runScriptlet("puts 'Hello from Ruby'"); }}

(defn -main "Call JRuby!" [& args] (doto (org.jruby.embed.ScriptingContainer.) (.runScriptlet "puts 'Hello from Ruby!'")))

Page 51: JRuby @ Boulder Ruby

JRuby + Clojure

Page 52: JRuby @ Boulder Ruby

;; examples.clj(def hmap (hash-map "foo" "FOO" "bar" "BAR" "baz" "BAZ"))

(println hmap)(println (get hmap "bar"))(println (contains? hmap "foo"))

Page 53: JRuby @ Boulder Ruby

# rubyJava::clojure.main.main(["./examples.clj"])

Page 54: JRuby @ Boulder Ruby

github.com/daveray/familiar

Page 55: JRuby @ Boulder Ruby

Familiar.with do # clojure.core functions here # ‘-’ replaced with ‘_’ # e.g. hash-map => hash_mapend

Page 56: JRuby @ Boulder Ruby

Familiar.with do r = ref(nil)

begin ref_set r, 'foo' rescue puts "no transaction! #{$!}" end

dosync do ref_set r, 'foo' end p deref(r)end

Page 57: JRuby @ Boulder Ruby

j.mp/clojure-

from-jruby

Page 58: JRuby @ Boulder Ruby

FUTURE

Page 59: JRuby @ Boulder Ruby
Page 60: JRuby @ Boulder Ruby

{,bugs,wiki,ci}jruby.org

Page 61: JRuby @ Boulder Ruby

github.com/jruby

Page 62: JRuby @ Boulder Ruby

Thanks!