1<br />JRuby in Java Projects<br />Denis Lutz<br />
2<br />Motivation: Problem, Solution<br />JRuby as language<br />Java and Ruby Integration <br />Ruby Community<br />Show ...
3<br />Motivation<br />
4<br />Lets redefine our thinking<br />What is our task?<br />What is the shortest path to the solution?<br />Since…<br />...
5<br />Java has a huge set of tools<br />JVM scripting languages are present (Groovy)<br />You can find a tool for everyth...
6<br />Projectwide scripting support not the rule, more an exception<br />Ant = scripting language =>  XML is our scriptin...
7<br />Entering JRuby<br />
8<br />scripting tool as one of main and known tools<br />scripting language as the surrounding environment and entry poin...
9<br />What is JRuby<br /><ul><li>JRuby = implementation of Ruby in Java
Runs in JVM and integrates perfectly into Java environments
very expressive
mature
huge community powered by the Ruby on Rails framework</li></li></ul><li>10<br />Front End (MVC)<br />Build management<br /...
11<br />Facets of JRuby<br />
12<br />Download Jruby at www.jruby.org<br />Extract<br />Add to  $PATH<br />Test your installation with “jruby –v” on the...
13<br />Simple Syntax<br />Everything is an object<br />Blocks<br />Self contained ( contains most useful libs out of the ...
14<br />Simple object creation<br />
15<br />Create your class and use it, lightweight and easy!<br />
16<br />Everything is an object<br />
17<br />Blocks, the most amazing and powerful feature ever<br />
18<br />Lets call JRuby from Java<br />public void callJRuby() {<br />  ScriptEngineManager m = new ScriptEngineManager();...
19<br />require 'java’<br />java_import java.lang.System<br /> => Java::JavaLang::System <br />version = System.getPropert...
20<br />Ruby’s Community<br />
21<br />Very strong community, why?<br />Got powered by Ruby on Rails (MVC Framework)<br />Fun oriented community<br />Mot...
22<br />Endless amounts of gems<br />
23<br />Find most popular gems in one place, rated by the community<br />
24<br />Live Demo: Using a GEM<br />
25<br />we want to communicate with a REST API<br />should be usable within a build<br />or inside a java class of course<...
26<br />Choose your gem<br />HTTParty<br />Choose HTTParty as the gem for REST communication<br />
27<br />Install the GEM<br />
28<br />Write our nice little class<br />
29<br />Use it immidiatelly on the console to play with it<br />
30<br />We knew from one site about the best fitting GEM for our task<br />We had no troubles installing it<br />I can try...
31<br />Integration with Java<br />
32<br />Yes its right, Rails got over JSF (java standard) in the meanwhile<br />Is this still a new and not known technolo...
33<br />Yes, possible and widely used<br />JRuby on Rails Frontend as war file in Tomcat<br />Rails Front Ends in Java Pro...
34<br />Java Build with Rake<br />
35<br />Rake as build tool in Java projects<br />Basic problem:<br />ANT is accepted, Maven the „new standard“<br />None o...
36<br />Projectsetup, its in the Wiki, well because Ant cant do it ;-) <br />Folders and File management<br />„if“ – „else...
37<br />Full language power in your build, not XML<br />We can still call our old Ant tasks<br />UNIX operations as if we ...
38<br />Rake does all you want :<br />project setup<br />methods<br />operating system calls<br />objects in your build<br...
39<br />Rake in 3 mins, nothing is easier!<br />
40<br />Call classic and custom Ant tasks from rake if needed<br />
41<br />Operating system communication<br />File management<br />Folder management<br />Talk to your operating system as f...
42<br />Some rake possibilities = just full ruby power<br />
43<br />In/Out API for your project<br />
44<br />Its difficult to process files as well as different formats in java<br />Import or Export is mostly a focused sing...
45<br />JRuby as your In/Out API of any project<br />
46<br />(Integration) Testing<br />
47<br />Integration tests are the best candidate to do it with JRuby<br />Abstract, mimal input, very high coverage<br />A...
48<br />Webrat Example<br />
49<br />GUI Testing API<br />Evaluates the pure HTML output<br />No browser setup or dependency to run your tests<br />Wri...
50<br />Expected Result from <br />Jruby integration<br />
51<br />All mentioned task areas can be done easier in JRuby<br />More choices of tools, as would it be only with Java<br ...
Upcoming SlideShare
Loading in …5
×

JRuby in Java Projects

5,657 views

Published on

My goals have been:

- focusing on several project areas, where you can use jruby successfully
- share the experience that I made using ruby in the last years
- proove that things can be done easier as they are done in typical java projects

Published in: Technology
  • Be the first to comment

  • Be the first to like this

JRuby in Java Projects

  1. 1. 1<br />JRuby in Java Projects<br />Denis Lutz<br />
  2. 2. 2<br />Motivation: Problem, Solution<br />JRuby as language<br />Java and Ruby Integration <br />Ruby Community<br />Show Case Demo<br />Usage areas for JRuby<br />Agenda<br />
  3. 3. 3<br />Motivation<br />
  4. 4. 4<br />Lets redefine our thinking<br />What is our task?<br />What is the shortest path to the solution?<br />Since…<br />Not the solving of complicated tasks is the goal<br />…but solving our task in the easiest possible way<br />Motivation<br />
  5. 5. 5<br />Java has a huge set of tools<br />JVM scripting languages are present (Groovy)<br />You can find a tool for everything<br />The java magazines are writing about scripting languages<br />Theory<br />
  6. 6. 6<br />Projectwide scripting support not the rule, more an exception<br />Ant = scripting language => XML is our scripting language<br />If scripting language integration => Ant / Spring is the environment<br />Adhoc scripting areas are randomly used by single devs<br />Instead a major scripting support strategy is missing<br />Young developers face only java or ant <br />Young devs are on their own to find more expressive languages<br />Real world praxis<br />
  7. 7. 7<br />Entering JRuby<br />
  8. 8. 8<br />scripting tool as one of main and known tools<br />scripting language as the surrounding environment and entry point (not Ant)<br />clearly decleared scripting language standard<br />definition of taks areas for the scripting language<br />easy and powerfull communication with the underlying operating system<br />powerfull language features instead of XML tools (Ant, Maven)<br />knowledge reuse because of definition of several scripting areas in a projekt<br />from GUIs and IDEs to automation and console procedures<br />from enterprisy java community to a fun community in ruby<br />Goal<br />
  9. 9. 9<br />What is JRuby<br /><ul><li>JRuby = implementation of Ruby in Java
  10. 10. Runs in JVM and integrates perfectly into Java environments
  11. 11. very expressive
  12. 12. mature
  13. 13. huge community powered by the Ruby on Rails framework</li></li></ul><li>10<br />Front End (MVC)<br />Build management<br />Testing, specifically integration testing<br />Import/Export APIs<br />Code generation<br />We can use JRuby in many project areas<br />
  14. 14. 11<br />Facets of JRuby<br />
  15. 15. 12<br />Download Jruby at www.jruby.org<br />Extract<br />Add to $PATH<br />Test your installation with “jruby –v” on the console<br />Command execution with<br />“jruby –S ‘command’ ”<br />jruby –S irb -> Will start the JRuby console<br />Get started with JRuby in 3 mins…<br />
  16. 16. 13<br />Simple Syntax<br />Everything is an object<br />Blocks<br />Self contained ( contains most useful libs out of the box)<br />Open classes<br />Principle of least surprise<br />Most imressive language features<br />
  17. 17. 14<br />Simple object creation<br />
  18. 18. 15<br />Create your class and use it, lightweight and easy!<br />
  19. 19. 16<br />Everything is an object<br />
  20. 20. 17<br />Blocks, the most amazing and powerful feature ever<br />
  21. 21. 18<br />Lets call JRuby from Java<br />public void callJRuby() {<br /> ScriptEngineManager m = new ScriptEngineManager(); ScriptEngine rubyEngine = m.getEngineByName("jruby"); if (rubyEngine==null) throw new RuntimeException("Did not find my ruby engine"); ScriptContext context = rubyEngine.getContext(); context.setAttribute("world","Programmierer",ScriptContext.ENGINE_SCOPE); try{ File f = new File("hello1.rb"); BufferedReader br = new BufferedReader(new FileReader(f)); rubyEngine.eval(br, context); // (1) } catch (ScriptException e) { e.printStackTrace(); } catch (FileNotFoundException fnfe) { System.err.println(fnfe.getMessage()); }}<br />
  22. 22. 19<br />require 'java’<br />java_import java.lang.System<br /> => Java::JavaLang::System <br />version = System.getProperties["java.runtime.version"]<br /> => "1.6.0_17-b04-248-10M3025” <br />import java.util.ArrayList<br />list = ArrayList.new<br /> => #<Java::JavaUtil::ArrayList:0x2bf09a31> <br />ruby-1.8.7-p334 :042 > iterator = list.iterator<br /> => #<#<Class:01x41a7c484>:0x367c218e> <br />ruby-1.8.7-p334 :043 > iterator.java_class<br /> => class java.util.AbstractList$Itr <br />ruby-1.8.7-p334 :044 > list.get(1)<br />NativeException: java.lang.IndexOutOfBoundsException: Index: 1, Size: 0<br />Lets call Java from Jruby und use your legacy systems<br />
  23. 23. 20<br />Ruby’s Community<br />
  24. 24. 21<br />Very strong community, why?<br />Got powered by Ruby on Rails (MVC Framework)<br />Fun oriented community<br />Motivated hobby programmers, who can be 9-5 employees ;-)<br />Get into positive cycle of beeing threated well and wanting more ruby<br />GEMS = ruby libraries, created by the community<br />Community<br />
  25. 25. 22<br />Endless amounts of gems<br />
  26. 26. 23<br />Find most popular gems in one place, rated by the community<br />
  27. 27. 24<br />Live Demo: Using a GEM<br />
  28. 28. 25<br />we want to communicate with a REST API<br />should be usable within a build<br />or inside a java class of course<br />Should provide an easy API for our java class<br />The task<br />Easiest solution (a base for it) ?<br />
  29. 29. 26<br />Choose your gem<br />HTTParty<br />Choose HTTParty as the gem for REST communication<br />
  30. 30. 27<br />Install the GEM<br />
  31. 31. 28<br />Write our nice little class<br />
  32. 32. 29<br />Use it immidiatelly on the console to play with it<br />
  33. 33. 30<br />We knew from one site about the best fitting GEM for our task<br />We had no troubles installing it<br />I can try my written code on the console<br />it just works, doesn’t matter what you install<br />its my experience after two years using ruby, yours will be the same<br />Why was this great?<br />
  34. 34. 31<br />Integration with Java<br />
  35. 35. 32<br />Yes its right, Rails got over JSF (java standard) in the meanwhile<br />Is this still a new and not known technology for you?<br />Rails (Ruby) popularity<br />
  36. 36. 33<br />Yes, possible and widely used<br />JRuby on Rails Frontend as war file in Tomcat<br />Rails Front Ends in Java Projects possible <br />everything else is waste of money and developers frustration<br />No risk, tell to your java influenced boss: „its just a war file“ <br />No server infrastructure changes needed<br />Deploy a Ruby on Rails Application in a java war<br />jruby -S gem install -y rails warbler<br />$RAILS_APP_ROOT/jruby -S warble war<br />
  37. 37. 34<br />Java Build with Rake<br />
  38. 38. 35<br />Rake as build tool in Java projects<br />Basic problem:<br />ANT is accepted, Maven the „new standard“<br />None of them is sufficient for the task we want to do<br />Why?<br />
  39. 39. 36<br />Projectsetup, its in the Wiki, well because Ant cant do it ;-) <br />Folders and File management<br />„if“ – „else“ , can you declare it out of your head now in Ant? <br />Declare a method in your build? Oh, yea no problem let me just look into Ant API<br />Loading of fixtures, possible with some XML setup again<br />Server startup? I am doing it by hand and each new developer has to learn the specifics <br />Maven „the biggest hype“ that helps me even less then Ant<br />Problems with Ant and others…<br />
  40. 40. 37<br />Full language power in your build, not XML<br />We can still call our old Ant tasks<br />UNIX operations as if we would be on the console<br />Method declarations, as simple as possible<br />Reuse of a language we already use<br />not learning new XML frameworks<br />What we want<br />
  41. 41. 38<br />Rake does all you want :<br />project setup<br />methods<br />operating system calls<br />objects in your build<br />file operations<br />Ivy integration<br />Rake is the solution, rubys build tool<br />
  42. 42. 39<br />Rake in 3 mins, nothing is easier!<br />
  43. 43. 40<br />Call classic and custom Ant tasks from rake if needed<br />
  44. 44. 41<br />Operating system communication<br />File management<br />Folder management<br />Talk to your operating system as from the console<br />
  45. 45. 42<br />Some rake possibilities = just full ruby power<br />
  46. 46. 43<br />In/Out API for your project<br />
  47. 47. 44<br />Its difficult to process files as well as different formats in java<br />Import or Export is mostly a focused single task<br />Can be done separated, by one developer<br />No requirement to do it in java<br />Customer data import is a very common and important task<br />Use JRuby to provide an import / export API<br />Data processing much faster<br />Easy CSV, Excel, XML processing <br />Generate projects specific formats for import<br />JRuby as your In/Out API of any project<br />
  48. 48. 45<br />JRuby as your In/Out API of any project<br />
  49. 49. 46<br />(Integration) Testing<br />
  50. 50. 47<br />Integration tests are the best candidate to do it with JRuby<br />Abstract, mimal input, very high coverage<br />Are easy to keep out of the java environment system<br />Can be perfectly done with pure ruby<br />WEBRAT GEM as JRuby Library<br />Integration Testing <br />
  51. 51. 48<br />Webrat Example<br />
  52. 52. 49<br />GUI Testing API<br />Evaluates the pure HTML output<br />No browser setup or dependency to run your tests<br />Write your tests fast in ruby <br />Cover the complete application workflow with minimal effort<br />Can be run automatically in backgroud while developing<br />Excellent to give fast feedback about application stability<br />Webrat<br />
  53. 53. 50<br />Expected Result from <br />Jruby integration<br />
  54. 54. 51<br />All mentioned task areas can be done easier in JRuby<br />More choices of tools, as would it be only with Java<br />Knowledge reuse in different project areas<br />Work is getting more lightweigt<br />From java IDEs to fast editors<br />Perfect interaction between different parts of the build<br />Expected result<br />
  55. 55. 52<br />My personal, subjective impression<br />You will reach more, having to know less<br />Achieve more with less work using ruby<br />
  56. 56. 53<br />http://www.jruby.org/<br />http://kenai.com/projects/jruby/pages/Home<br />Jruby console with “jruby –S irb”<br />http://www.cygwin.com/ (Unix Console replacement for Windows)<br />Editors<br />Textmate (MacOs)<br />http://www.e-texteditor.com/ (Windows)<br />IDE’s<br />http://www.jetbrains.com/ruby/<br />http://www.aptana.com/products/radrails (Eclipse Plugin)<br />http://wiki.netbeans.org/Ruby (Netbeans Ruby Support)<br />Further informations<br />
  57. 57. 54<br />Thanks a lot and have fun with JRuby!<br />

×