Why would a Java shop want to use Ruby?

  • View
    5.297

  • Download
    0

Embed Size (px)

DESCRIPTION

Briefly explores several practical reasons that may encourage Java shops to explore Ruby.

Text of Why would a Java shop want to use Ruby?

  • 1.Why would a Java Shopwant to use Ruby? Keith Pitty Open Source Developers Conference Brisbane29 November 2007

2. About Keith Programming professionally since 1983 Early experience on mainframes Smalltalk was my rst OO language Employed by a Java consultancy since 2000 Fascinated by Ruby and Rails since 2004 Enjoy sampling a wide range of beers Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 3. Java Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 4. Ruby Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 5. Java public class print { public static void main(String[] args) { System.out.println(quot;Hi, I'm Javaquot;); } }Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 6. Ruby#!/usr/bin/env rubyputs quot;Hi, I'm Rubyquot; Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 7. No!!! This is not a language war!Java and Ruby both have their strengthsIm not interested in Ruby beating Java The question is:Can Ruby help Java programmers?Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 8. Java Most popular language (21.7%) TIOBE PCI, September 2007 Mature platform Trusted in the enterprise Has reached a plateau The new COBOLWhy would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 9. Ruby Tenth most popular language (2.1%) Marked increase in popularity since mid-2006 due to Ruby on Rails Matzs original motivation: a scripting language more powerful than Perl more object-oriented than PythonWhy would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 10. Why Ruby?(Quote Part 1)I dont believe there would be any real technicalbenet. There may be a productivity boost, but when you consider how much time is used by developers, QA, etc... to bring in a new technology, chances are the net [benet] wont be that great. Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 11. Why Ruby? (Quote Part 2)I think the best argument for Ruby is that your developers want it. If the developers are yearning for new technology, its probablyworth considering. Happy developers code more and code better.Norman Richards, JBoss Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 12. Perspectives Vendors Open source contributors Managers Business owners Developers Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 13. Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 14. Ruby: Why not? How widespread is knowledge ofRuby within the Java community? In my experience, there islimited awareness On the other hand: Ruby may have benets that arecomplementary to JavaWhy would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 15. Potential Ruby Benets Ruby skills may complement Java skills Learning Ruby may be interesting,broadening a programmers thinking Happier programmers Quicker time to market Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 16. Enough Preamble What does Ruby have to offerpeople in Java development shops? Lets explore some specics... Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 17. Ruby Scripts Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 18. Ruby Scripts Scripts help with repetitive tasks Shell scripts, SQL scripts, Ant scripts Sometimes the logic lends itself to a general purpose language Can run a Java application via a shell script A Ruby script is interpreted directly and ismore conciseWhy would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 19. Ruby Script Example Deleting les within a givendirectory according to givencriteria e.g. older than a given timeWhy would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 20. Ruby Script Example* #!/usr/bin/env ruby def delete_if(dir) Dir.chdir(dir) do Dir.foreach(*.*) do |entry| next if File.stat(entry).directory? if yield entry File.unlink(entry) end end end enddelete_if(quot;/tmpquot;) { |f| File.mtime(f) < Time.local(2007,10,29,0,0,0) } * See Hal Fultons The Ruby WayWhy would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 21. Rake and Internal DSLsWhy would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 22. Rake Ruby-based build language Developed by Jim Weirach Similar to make and ant An internal Domain Specic Language make and ant are external DSLsWhy would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 23. Regular Rake Taskstask :init do# init actionsend task :compile => [:init] do# compile actionsend task :build =>[:init, :compile] do# build actionsend Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 24. Extending Rake* def copyTask srcGlob, targetDirSuffix, taskSymbol targetDir = File.join BUILD_DIR, targetDirSuffix mkdir_p targetDir, QUIET FileList[srcGlob].each do |f| target = File.join targetDir, File.basename(f) file target => [f] do |t| cp f, target end task taskSymbol => target end endcopyTask 'articles/*.gif', 'articles', :articles* see Martin Fowlers rake article Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 25. Internal DSLs Other Ruby internal DSLs: buildr capistrano ... create your own! Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 26. Buildr a build system for Java apps encompasses Maven and Ant written in Ruby based on Rake uses Antwrap to congure and run Ant tasks extensible in RubyWhy would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 27. Capistrano Created by Jamis Buck Originally to ease the pain ofdeploying Rails applications Exposes simple commands Once project has been capied $ cap deploy Useful beyond RailsWhy would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 28. Capistrano Example desc quot;Copy production database config file.quot; namespace :deploy do task :after_update_code, :roles => :app do db_config = quot;#{shared_path}/config/database.yml.productionquot; run quot;cp #{db_config} #{release_path}/config/database.ymlquot; end end Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 29. Metaprogramming Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 30. Metaprogramming Very powerful feature of Ruby Dynamic programming Methods: method_missing, define_method,module_eval, class_eval, instance_eval,self.included, self.inherited, extend Callable objects: procs, lambdas, blocks Modifying the singleton class: class 4 r.xxiii #=> 23 r.mm#=> 2000 Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 32. More Examples? Open source Ruby frameworks e.g. Ruby on Rails web applicationframework many examples ofmetaprogramming to be foundin the Rails source codeWhy would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 33. Ruby on RailsWhy would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 34. Ruby on Rails Web application framework released byDavid Heinemeier Hansson in 2004 Extracted from the Basecamp application The killer app for Ruby Convention over Conguration Code generatorsWhy would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 35. Rails Example$ rails beers Creates directory structure for new project Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 36. Generating a Resource $ ./script/generate scaffold_resource beer name:stringCreates a model, a controller, and a set oftemplates that's ready to use as thestarting point for your REST-like,resource-oriented application. Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 37. Migration class CreateBeers < ActiveRecord::Migration def self.up create_table :beers do |t| t.column :name, :string end enddef self.down drop_table :beers end endWhy would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 38. Running Migrations$ rake db:migrate== CreateBeers: migrating ===================================================== -- create_table(:beers)-> 0.0735s == CreateBeers: migrated (0.0737s) ============================================ Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 39. Model class Beer < ActiveRecord::Basehas_many :votesend Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 40. View (new.rhtml) New beer beers_path) do |f| %>

Name

Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 41. Controller class BeersController < ApplicationControllerdef new @beer = Beer.new enddef create @beer = Beer.new(params[:beer]) respond_to do |format| if @beer.save flash[:notice] = 'Beer was successfully created.' format.html { redirect_to beer_url(@beer) } else format.html { render :action => quot;newquot; } end end endend Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 42. More about Rails Theres much more about Rails to exploreBut thats another topic Weve touched upon some appealingaspects Development is much quicker! But ...Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 43. Java Perspective What if ... Java provides where Ruby doesnt? What about ... deploying Rails apps?Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 44. Java and Ruby Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 45. JRuby Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 46. JRuby Java implementation of Ruby Major advantages: Java libraries can be used with Ruby syntax Deploy Rails apps on Java app serversvia WAR les Why would a Java Shop want to use Ruby? Keith Pitty OSDC, 29 November 2007 47. Invoking Java from Rub