Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Like this presentation? Why not share!

JavaOne 2010 - JRuby

on

  • 1,536 views

 

Statistics

Views

Total Views
1,536
Views on SlideShare
1,536
Embed Views
0

Actions

Likes
0
Downloads
10
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />

JavaOne 2010 - JRuby JavaOne 2010 - JRuby Presentation Transcript

  • Hands-on JRuby: Making Your Job Easy Thomas Enebo, Charles Nutter Engine Yard
  • Goal Get an idea of how JRuby can help you in day-to-day development
  • Introduction JRuby Guys 4 Years as our “day job” We still eat and breath Java but drink beer 15 years and still here
  • http://www.pragprog.com/titles/jruby/using-jruby
  • JRuby @ 10_000 Implementation of Ruby runtime that runs on a JVM http://www.jruby.org/ OpenSource (CPL, LGPL, GPL) Requires Java 5+ Many Features ...
  • Ruby Quick Tour The relative feel between Java and Ruby
  • Classes public class Circle extends Shape { class Circle < Shape private final int radius; def initialize(radius) @radius = radius public Circle(int radius) { end this.radius = radius; } attr_reader :radius public int getRadius() { return radius; def area } Math::PI*(@radius ** 2) public double getArea() { end return Math.PI * Math.pow(radius,2); end } public static void main(String[] a) { puts Circle.new(2).area double area=new Circle(2).getArea(); System.out.println(area); } }
  • Single Inheritance Same Thing public class Circle extends Shape { class Circle < Shape private final int radius; def initialize(radius) @radius = radius public Circle(int radius) { end this.radius = radius; } attr_reader :radius public int getRadius() { return radius; def area } Math::PI*(@radius ** 2) public double getArea() { end return Math.PI * Math.pow(radius,2); end } public static void main(String[] a) { puts Circle.new(2).area double area=new Circle(2).getArea(); System.out.println(area); } }
  • Constructor public class Circle extends Shape { class Circle < Shape private final int radius; def initialize(radius) @radius = radius public Circle(int radius) { end this.radius = radius; } attr_reader :radius public int getRadius() { return radius; def area } Math::PI*(@radius ** 2) public double getArea() { end return Math.PI * Math.pow(radius,2); end } public static void main(String[] a) { puts Circle.new(2).area double area=new Circle(2).getArea(); System.out.println(area); } }
  • No Type Declarations public class Circle extends Shape { class Circle < Shape private final int radius; def initialize(radius) @radius = radius public Circle(int radius) { end this.radius = radius; } attr_reader :radius public int getRadius() { return radius; def area } Math::PI*(@radius ** 2) public double getArea() { end return Math.PI * Math.pow(radius,2); end } public static void main(String[] a) { puts Circle.new(2).area double area=new Circle(2).getArea(); System.out.println(area); } }
  • Dynamic Typing Ruby is Dynamically Typed Why we see no Type decls on variables Runtime type-checking (like Java casts) “If it waddles like a duck and quacks like a duck...”
  • Instance Variables ‘@’ == ‘this.’ and is mandotory public class Circle extends Shape { class Circle < Shape private final int radius; def initialize(radius) @radius = radius public Circle(int radius) { end this.radius = radius; } attr_reader :radius public int getRadius() { return radius; def area } Math::PI*(@radius ** 2) public double getArea() { end return Math.PI * Math.pow(radius,2); end } public static void main(String[] a) { puts Circle.new(2).area double area=new Circle(2).getArea(); System.out.println(area); } }
  • No Returns Last Expression is always return value public class Circle extends Shape { class Circle < Shape private final int radius; def initialize(radius) @radius = radius public Circle(int radius) { end this.radius = radius; } attr_reader :radius public int getRadius() { return radius; def area } Math::PI*(@radius ** 2) public double getArea() { end return Math.PI * Math.pow(radius,2); end } public static void main(String[] a) { puts Circle.new(2).area double area=new Circle(2).getArea(); System.out.println(area); } }
  • .new is just a class method! public class Circle extends Shape { class Circle < Shape private final int radius; def initialize(radius) @radius = radius public Circle(int radius) { end this.radius = radius; } attr_reader :radius public int getRadius() { return radius; def area } Math::PI*(@radius ** 2) public double getArea() { end return Math.PI * Math.pow(radius,2); end } public static void main(String[] a) { puts Circle.new(2).area double area=new Circle(2).getArea(); System.out.println(area); } }
  • Blocks/Closures Pass an anonymous function to a method call # Look mom...no boilerplate! my_open(file) do |io| first_line = io.gets # ... end def my_open(file, mode='r') io = File.open(file) yield io ensure io.close end
  • JRuby Features Call into Ruby from Java Java 6 Scripting Internal “Red Bridge” API Access Java classes from Ruby Call, Decorate, and Define in Ruby
  • Access Ruby from Java ScriptingContainer container = new ScriptingContainer(); // ... container.put("$revisions", new Revisions(args[0], args[1])); List<Diff> f = (List<Diff>) container.runScriptlet("history"); for (GitDiff file: f) { System.out.println("FILE: " + file.getPath()); System.out.println(file.getPatch()); }
  • Use Case: Scripting Java One-off scripts are easy as opening an editor Not everything must be an IDE project Easy to play with technology
  • Demo: Java from Ruby
  • Demo: Scripting Java
  • Ant + Rake Ant is 800 pound Gorilla of Java Declarative and a little Dumb but Dependable Used Everywhere
  • Ant + Rake Rake is Orangutang of Ruby Like Make or Ant ....but with Ruby + Imperative Goodness
  • Ant + Rake Call Rake From Ant Call tasks Rake tasks as Ant target dependencies Call Ant From Rake Ditto! Mix and Match
  • Rake Calling Ant Tasks require 'rake' require 'ant' task :init do ant.mkdir :dir => 'build' end task :compile => :init do ant.javac :destdir => 'build' do src { pathelement :location => 'src' } end end task :test => :some_ant_task
  • Rake Calling Ant task :call_ant do ant '-f my_build.xml its_in_ant' end task :ant_import do ant_import # you could also do this outside end task :compile => [:ant_import, :its_in_ant] do # Do some compilation end
  • Rake from Ant <?xml version="1.0" encoding="UTF-8"?> <project name="foobar" default="default" basedir="."> <description>Builds, tests, and runs the project foobar. </description> <target name="load-rake-task"> <taskdef name="rake" classname="org.jruby.ant.Rake"/> </target> <target name="default" depends="load-rake-task"> <rake task="jar"/> </target> ... </project>
  • Ant Calling Rake Tasks <?xml version="1.0" encoding="UTF-8"?> <project name="foobar" default="top-level" basedir="."> <description>Builds the project foobar.</description> <taskdef name="rakeimport" classname="org.jruby.ant.RakeImport"/> <rakeimport/> <target name="top-level" depends="its_in_rake" /> <target name="its_in_ant"> <echo message="ant: its_in_ant"/> </target> </project>
  • Ruby on Rails Still setting the bar for web frameworks RESTful services HTML 5 JQuery Rails 3 Cleanups, improvements, features
  • JRuby on Rails Deploy Rails on any Java server WARfile-based “lightweight” command-line server Use all the Java libs you have today Best of both worlds!
  • Demo: Rails Simple
  • Demo: Rails + Java
  • Demo: Rails Deployment
  • JRuby Meetup! Tuesday, September 21 5:30 pm – 7:00 pm Networking, beer and food 7:00 pm – 8:00 pm Lightning talks Engine Yard, Inc. 500 Third Street (@ Bryant Street) Suite 510 San Francisco, CA 94107
  • Conclusion Ruby is easy to learn for Java programmers Easy to mix Ruby+Java Useful Productivity Frameworks and Libraries Rails, Rake One more tool for Java programmer’s toolbelt
  • Other Talks Script Bowl (Going on right now) Fast Web Applications Development with Ruby on Rails on Oracle Hotel Nikko, Golden Gate, 2:30 Today PL/SQL Unit Testing Can Be Fun! Hotel Nikko, Bay View, 9:30 Tomorrow
  • Thank You twitter: headius, tom_enebo website: http://www.jruby.org/ blogs: blog.headius.com, http://blog.enebo.com
  • Demo: 3D Java from Ruby