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!

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,626
On Slideshare
1,626
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
10
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide





































Transcript

  • 1. Hands-on JRuby: Making Your Job Easy Thomas Enebo, Charles Nutter Engine Yard
  • 2. Goal Get an idea of how JRuby can help you in day-to-day development
  • 3. Introduction JRuby Guys 4 Years as our “day job” We still eat and breath Java but drink beer 15 years and still here
  • 4. http://www.pragprog.com/titles/jruby/using-jruby
  • 5. 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 ...
  • 6. Ruby Quick Tour The relative feel between Java and Ruby
  • 7. 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); } }
  • 8. 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); } }
  • 9. 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); } }
  • 10. 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); } }
  • 11. 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...”
  • 12. 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); } }
  • 13. 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); } }
  • 14. .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); } }
  • 15. 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
  • 16. 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
  • 17. 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()); }
  • 18. 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
  • 19. Demo: Java from Ruby
  • 20. Demo: Scripting Java
  • 21. Ant + Rake Ant is 800 pound Gorilla of Java Declarative and a little Dumb but Dependable Used Everywhere
  • 22. Ant + Rake Rake is Orangutang of Ruby Like Make or Ant ....but with Ruby + Imperative Goodness
  • 23. Ant + Rake Call Rake From Ant Call tasks Rake tasks as Ant target dependencies Call Ant From Rake Ditto! Mix and Match
  • 24. 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
  • 25. 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
  • 26. 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>
  • 27. 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>
  • 28. Ruby on Rails Still setting the bar for web frameworks RESTful services HTML 5 JQuery Rails 3 Cleanups, improvements, features
  • 29. 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!
  • 30. Demo: Rails Simple
  • 31. Demo: Rails + Java
  • 32. Demo: Rails Deployment
  • 33. 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
  • 34. 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
  • 35. 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
  • 36. Thank You twitter: headius, tom_enebo website: http://www.jruby.org/ blogs: blog.headius.com, http://blog.enebo.com
  • 37. Demo: 3D Java from Ruby