Top Banner
Ola Bini computational metalinguist [email protected] http://olabini.com/blog 698E 2885 C1DE 74E3 2CD5 03AD 295C 7469 84AF 7F0C JRuby For The Win onsdag 12 juni 13
36

JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

Oct 17, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

Ola  Binicomputational  metalinguist

[email protected]  http://olabini.com/blog

698E  2885  C1DE  74E3  2CD5  03AD  295C  7469  84AF  7F0C

JRuby  For  The  Win

onsdag 12 juni 13

Page 2: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

Logistics  and  Demographics

onsdag 12 juni 13

Page 3: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

LAST  MINUTE  DEMO

onsdag 12 juni 13

Page 4: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

Implementation  of  the  Ruby  language

Java  1.6+

1.8.7  and  1.9.3  compatible  (experimental  2.0  support)

Open  Source

Created  2001

Embraces  testing

Current  version:  1.7.4

Support  from  EngineYard,  RedHat  &  ThoughtWorks

JRuby

onsdag 12 juni 13

Page 5: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

Threading

Unicode

Performance

Memory

Explicit  extension  API  and  OO  internals

Libraries  and  legacy  systems

Politics

Why  JRuby?

onsdag 12 juni 13

Page 6: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

InvokeDynamic

onsdag 12 juni 13

Page 7: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

Most  compatible  alternative  implementation

Native  threads  vs  Green  threads

No  C  extensions  (well,  some)

No  continuations

No  fork

ObjectSpace  disabled  by  default

JRuby  Differences

onsdag 12 juni 13

Page 8: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

Simple  JRuby

onsdag 12 juni 13

Page 9: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

Java  types  ==  Ruby  types

Call  methods,  construct  instances

Static  generation  of  classes

camelCase  or  snake_case

.getFoo(),  setFoo(v)  becomes  .foo  and  .foo  =  v

Interfaces  can  be  implemented

Classes  can  be  inherited  from

Implicit  closure  conversion

Extra  added  features  to  Rubyfy  Java

Java  integration

onsdag 12 juni 13

Page 10: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

Ant+Rake

onsdag 12 juni 13

Page 11: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

Clojure  STM

onsdag 12 juni 13

Page 12: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

Web

onsdag 12 juni 13

Page 13: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

Rails

onsdag 12 juni 13

Page 14: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

Sinatra

onsdag 12 juni 13

Page 15: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

Trinidad

onsdag 12 juni 13

Page 16: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

SwingSwing  API  ==  large  and  complex

Ruby  magic  simplifies  most  of  the  tricky  bits

Java  is  a  very  verbose  language

Ruby  makes  Swing  fun  (more  fun  at  least)

No  consistent  cross-­‐platform  GUI  library  for  Ruby

Swing  works  everywhere  Java  does

onsdag 12 juni 13

Page 17: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

Swing  -­‐  the  direct  approachjava_import javax.swing.JFramejava_import javax.swing.JButton

frame = JFrame.new("Swing is easy now!")frame.set_size 300, 300frame.always_on_top = true

button = JButton.new("Press me!")button.add_action_listener do |evt| evt.source.text = "Don't press me again!" evt.source.enabled = falseend

frame.add(button)frame.show

onsdag 12 juni 13

Page 18: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

Swing  -­‐  Cheri  (builder)include Cheri::Swing

frame = swing.frame("Swing builders!") { |form| size 300, 300 box_layout form, :Y_AXIS content_pane { background :WHITE }

button("Event binding is nice") { |btn| on_click { btn.text = "You clicked me!" } }}

frame.visible = true

onsdag 12 juni 13

Page 19: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

Swing  -­‐  Profligacyclass ProfligacyDemo java_import javax.swing.* include Profligacy

def initialize layout = "[<translate][*input][>result]" @ui = Swing::LEL.new(JFrame, layout) {|cmps, ints| cmps.translate = JButton.new("Translate") cmps.input = JTextField.new cmps.result = JLabel.new

translator = proc {|id, evt| original = @ui.input.text translation = MyTranslator.translate(original) @ui.result.text = translation }

ints.translate = {:action => translator} } endend

onsdag 12 juni 13

Page 20: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

Swing  -­‐  MonkeyBars  (tools)GUI  editor  friendly  (e.g.  NetBeans  “Matisse”)

Simple  Ruby  MVC  based  API

Combines  best  of  both  worlds

onsdag 12 juni 13

Page 21: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

TestingRuby  frameworks

Cucumber

JtestR

onsdag 12 juni 13

Page 22: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

The  problem  with  mockingIt  works  fine  while  staying  in  Ruby-­‐land

Setting  expectations  to  be  consumed  by  Java  is  problematic

onsdag 12 juni 13

Page 23: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

The  interface

package org.test;

public interface Quux { String one(); String two();}

onsdag 12 juni 13

Page 24: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

The  class

package org.test;

public class Foo implements Quux { public String one() { return "hello"; }

public String two() { return "goodbye"; }}

onsdag 12 juni 13

Page 25: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

The  consumerpackage org.test;

public class Bar { public void doFoo(Foo foo) { System.out.println("Foo one: " + foo.one()); System.out.println("Foo two: " + foo.two()); }

public void doQuux(Quux q) { System.out.println("Quux one: " + q.one()); System.out.println("Quux two: " + q.two()); }}

onsdag 12 juni 13

Page 26: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

Mocking  an  interface

describe "Foo and Bar with RSpec mocking" do it "can mock out parts of interface methods with RSpec" do f = org.test.Quux.new b = org.test.Bar.new f.should_receive(:one).and_return "Canned answer 1 from RSpec" f.should_receive(:two).and_return "Canned answer 2 from RSpec"

b.do_quux f endend

onsdag 12 juni 13

Page 27: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

Mocking  a  class

describe "Foo and Bar with RSpec mocking" do it "can mock out parts of instance methods with RSpec" do f = org.test.Foo.new b = org.test.Bar.new f.should_receive(:one).and_return "Canned answer 1 from RSpec" f.should_receive(:two).and_return "Canned answer 2 from RSpec"

b.do_foo f endend

onsdag 12 juni 13

Page 28: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

The  output

Quux one: Canned answer 1 from RSpecQuux two: Canned answer 2 from RSpec.Foo one: helloFoo two: goodbyeF

1)Spec::Mocks::MockExpectationError in 'Foo and Bar with RSpec mocking can mock out parts of instance methods with RSpec'org.test.Foo@7570b819 expected :one with (any args) once, but received it 0 times./rspec_mocking_spec.rb:18:

Finished in 0.321 seconds

2 examples, 1 failure

onsdag 12 juni 13

Page 29: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

The  JtestR  solution

onsdag 12 juni 13

Page 30: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

CucumberBehavior  driven  development

Plain  text  stories

Used  to  describe  high  level  behavior

“Business-­‐readable  domain  specific  language”

Serves  as  documentation,  automated  tests  and  dev  aid

Supports  table-­‐based  tests

onsdag 12 juni 13

Page 31: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

Erlang+Ruby

onsdag 12 juni 13

Page 32: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

Google  AppEngineJRuby  runs  on  it

JRuby-­‐rack  supports  it

Google  gems

Startup  time

Merb,  Ramaze  and  Sinatra  easy  options

Rails  works

onsdag 12 juni 13

Page 33: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

Mobile

onsdag 12 juni 13

Page 34: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

Usage

onsdag 12 juni 13

Page 35: JRubyForThe$ Winwinstonyw.com/assets/downloads/JRubyForTheWin.pdf · Swing Swing$API$==$large$and$complex Ruby$magic$simplifies$most$of$the$tricky$bits Java$is$a$very$verbose$language

onsdag 12 juni 13