Jruby

1,599 views

Published on

Published in: Real Estate, Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,599
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
13
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Jruby

  1. 1. Jruby and Rails<br />Why on earth…?<br />Chris McCann<br />
  2. 2. Necessity is the a mother…<br />Wanted to build a prototype Rails application that could interact with complicated Excel spreadsheets<br />Store “input” data in plain old AR models<br />Pass that data to the spreadsheets <br />Recalculate and extract the values of complicated formulas<br />Allow user to download a “live” spreadsheet filled with their data<br />
  3. 3. The Easy Way…not<br />Use the ruby spreadsheet gem!<br />“Reading is easy!”<br />“Writing is easy!”<br />“You should be able to fill in Data to be evaluated by predefined Formulas”<br />Yes, yes I should…but alas, I cannot<br />“A ‘To Do’ in source code != functionality!”<br />
  4. 4. Whoa…spreadscript?<br />Need to manipulate or dynamically generate Excel spreadsheet files? YES!<br />Need to perform complex spreadsheet calculations on data stored in a database? YES!!<br />Tired of being restricted to providing users with CSV files when you really want formulas, formatting, and multiple sheets in a workbook? YES!! YES!! YES!!<br />Want your Perl, Python, or Tcl scripts to have access to the power…? WTF? Perl, Python, Tcl? NOOOOOOOOO!<br />
  5. 5. A different angle<br />Build a formula calculation engine in Ruby<br />Pass the model inputs to the engine<br />Calculate the formula values<br />Populate a spreadsheet with the value…<br />…and while I’m at it write the next Googlebook<br />
  6. 6. Desperation sets in…<br />Use Microsoft Excel Web Services API in Sharepoint to expose a web service that allows me to programmatically access and interact with Excel 2010 spreadsheets stored in a Sharepoint document repository<br />Microsoft?<br />Sharepoint?<br />Web service?<br />
  7. 7. Ask people smarter than me<br />Pragmatic Programmer’s “Rails Studio” Alumni mailing list<br />“Why not use JRuby – there’s probably a really nice Excel spreadsheet library out there in Java”<br />Hmmm…I know Java, corporations love Java, and corporations love Excel…maybe he’s on to something!<br />
  8. 8. JRuby - What?<br />jruby.org - “100% Pure-Java implementation of…Ruby”<br />Instead of the C-based Ruby we all know and love<br />
  9. 9. Why?<br />
  10. 10. And the Google says…<br />Search for “java excel spreadsheet”<br />
  11. 11. Does JRuby do Rails?<br />https://github.com/jruby/jruby/wiki/JRubyOnRails<br />You can use JRuby with Ruby on Rails. JRuby gives Rails the power and functionality of the Java Platform, providing it with:<br />Excellent garbage collection for endless uptimes<br />Hotspot profiled dynamic optimizations for great performance<br />Access to the Java ecosphere for additional technology options<br />Deployment to Java application servers for ubiquity <br />
  12. 12. How to get it?<br />rvm install jruby<br />By default it loads as ruby 1.8.7<br />To get ruby 1.9:<br />export JRUBY_OPTS="--1.9”<br />add an after_use hook (see rvm docs)<br />
  13. 13. One gotcha<br />Mac OS X 10.5.8 – has java 1.5, wasn’t working<br />Download Java 1.6 update from Apple<br />Still not working, java –version = 1.5.0_26<br />Applications > Utilities > Java Preferences<br />Move Java SE 6 to the top of “Java application versions” <br />Reloads the new version automagically (1.6.0_22 in my case)<br />
  14. 14. Cheat like a boss!<br />
  15. 15. Key features for using Java<br />include Java # provides bridge to JVM<br /> # and shortcut for core classes<br />require 'poi-3.7-20101029.jar’ # bring in the JARs<br />require 'poi-ooxml-3.7-20101029.jar’<br />class AnalysisAdapter<br />include_classjava.io.FileInputStream # to use core Java classes<br />include_classjava.io.FileOutputStream<br />include_classJava::org.apache.poi.xssf.usermodel.XSSFWorkbook<br /># to explicitly use classes from JARs<br />
  16. 16. Ruby syntax translation<br />Use Ruby syntax on Java objects:<br />wb_path= Rails.root.join('public', 'spreadsheets', sheet_name)<br />fis= FileInputStream.new(wb_path.to_s); <br />workbook = XSSFWorkbook.new(fis)<br />Instead of the Java style:<br />FileInputStreamfis= new FileInputStream(wb_path.to_s); <br />XSSFWorkbookworkbook = new XSSFWorkbook(fis)<br />
  17. 17. A quick demo<br />

×