JRuby, Ruby, Rails and You
on the Cloud

December 6th, 2010
Who am I?


Hiro Asari
• JRuby Support Engineer at Engine Yard
• hasari@engineyard.com




                                          2
Engine Yard


• Platform as a Service provider
• Ruby on Rails specialists
• http://www.engineyard.com




                                   3
http://thenetworkisthecomputer.com/
                                 4
WHAT IS RUBY ON RAILS?"
AND"
WHY RUBY ON RAILS?


            http://www.flickr.com/photos/88019192@N00/3277925689/


                                                                5
JVM Web Frameworks Surveys


• http://bit.ly/jvm-frameworks-matrix
• http://bit.ly/webmatrixsurveyresults




                                         6
Rails


           ü Is a web application
         framework written in Ruby 


                 http://www.flickr.com/photos/46799485@N00/4386602878


                                                                    7
Ruby quick facts
• Conceived by Yukihiro
  Matsumoto (a.k.a. Matz)
  in 1993
• Object-oriented
  dynamically-typed
  interpreted scripting
  language
• Influenced by Perl,
  Smalltalk, Eiffel and LISP
• External libraries are
  most frequently
  distributed as “gems”


                               8
Rails quick facts
• Conceived by David
  Heinemeier Hansson
  (a.k.a. DHH) in 2004
• Extracted from 37signals’
  “Basecamp” application
• Uses MVC design
  pattern
• Rails 3, released in
  August, 2010, uses
  “Bundler” to manage
  gems.



                              9
Who uses Rails?




                   10
Getting JRuby




http://jruby.org




                    11
NetBeans



http://netbeans.org




                       12
NetBeans (if you have it already)




                                     13
RubyMine



http://jetbrains.com/ruby




                             14
RVM (Ruby Version Manager)


• http://rvm.beginrescueend.com/
• rvm install jruby
• rvm use jruby




                                   15
Rails


         ü Gets you off and running
                    quickly




                                       16
17
jruby -S rails new cloudstock_demo
cd cloudstock_demo
vi Gemfile
jruby -S bundle install
jruby -S bundle exec glassfish




                                     18
$ jruby -S rails new cloudstock_demo -m http://jruby.org
      create
      create README

      create   app
      create   app/controllers/application_controller.rb
      create   app/helpers/application_helper.rb
      create   app/mailers
      create   app/models
      create   app/views/layouts/application.html.erb

      create   public
      create   public/404.html

      create   script/rails
      create   test
      create   test/fixtures

      create   test/test_helper.rb
      create   test/unit

       apply   http://jruby.org
       apply     http://jruby.org/templates/default.rb
        gsub       Gemfile

                                                           19
20
$ jruby script/rails generate scaffold user name:string
calling init (1017e6ac0)
      invoke active_record
      create    db/migrate/20101115150436_create_users.rb
      create    app/models/user.rb
      invoke    test_unit
      create      test/unit/user_test.rb
      create      test/fixtures/users.yml
       route resources :users
      invoke scaffold_controller
      create    app/controllers/users_controller.rb
      invoke    erb
      create      app/views/users
      create      app/views/users/index.html.erb
      create      app/views/users/edit.html.erb
      create      app/views/users/show.html.erb
      create      app/views/users/new.html.erb
      create      app/views/users/_form.html.erb
      invoke    test_unit
      create      test/functional/users_controller_test.rb
      invoke    helper
      create      app/helpers/users_helper.rb
      invoke      test_unit
      create        test/unit/helpers/users_helper_test.rb
      invoke stylesheets
      create    public/stylesheets/scaffold.css



                                                             21
$ cat db/migrate/20101115150436_create_users.rb
class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.string :name

      t.timestamps
    end
  end

  def self.down
    drop_table :users
  end
end


                                                  22
Rails


         ü Is descriptive
                         




                             23
class User < ActiveRecord::Base
  validates_length_of :name, :minimum => 5
end




                                             24
Rails


         ü Is tested




                         25
$ find test
test
test/fixtures
test/fixtures/users.yml
test/functional
test/functional/users_controller_test.rb
test/integration
test/performance
test/performance/browsing_test.rb
test/test_helper.rb
test/unit
test/unit/helpers
test/unit/helpers/users_helper_test.rb
test/unit/user_test.rb


                                           26
$ cat test/unit/user_test.rb
require 'test_helper'

class UserTest < ActiveSupport::TestCase
  test "bare user is invalid" do
    u = User.new
    assert !u.valid?
    assert u.errors[:name].any?
  end

  test "user with name shorter than 5 character is invalid" do
    u = User.new(:name => "hi")
    assert !u.valid?
    assert u.errors[:name].any?
  end

  test "user with name with at least 5 character is valid" do
    u = User.new(:name => "hello")
    assert u.valid?
  end
end

                                                                 27
Rails


         ü Is good for agile
              development 




                                28
Rails


         ü Makes you happy
                          




                              29
WHAT ABOUT…




               30
Accessing Java library from Rails


• Generating SVG with Batik
• http://xmlgraphics.apache.org/batik/




                                         31
Example 1: Generating SVG with Batik
   require 'java'
   require 'batik'

   java_import org.apache.batik.svggen.SVGGraphics2D
   java_import org.apache.batik.dom.GenericDOMImplementation

   dom = GenericDOMImplementation.getDOMImplementation
   svg_ns = 'http://www.w3.org/2000/svg'
   doc = dom.createDocument svg_ns, 'svg', nil
   svg_gen = SVGGraphics2D.new( doc )

   svg_gen.set_paint java.awt.Color.send(color)
   svg_gen.fill java.awt.Rectangle.new(10,10,100,100)
   svg_gen.set_paint java.awt.Color.send(text_color)
   svg_gen.draw_string @user.name, 25, 55

   out = java.io.StringWriter.new
   svg_gen.stream(out, true)
   render :inline => out.to_string                                   

                                                               32
Accessing Java library from Rails


• Generating PDF with iText
• http://itextpdf.com/




                                     33
Example 2: Generating PDF with iText
 require 'iText-5.0.5'
 pdf = com.itextpdf.text.Document.new
 para = com.itextpdf.text.Paragraph.new "Hello #{@user.name}"
 file = "#{::Rails.root.to_s}/tmp/pdfs/pdf_demo.pdf”
 out = java.io.FileOutputStream.new file

 com.itextpdf.text.pdf.PdfWriter.get_instance pdf, out
 pdf.open
 pdf.add para
 pdf.close

 render :file => file



              Don’t do this in production code!
                                              


                                                                34
JRuby on Engine Yard AppCloud




                                 35
JRuby on Engine Yard AppCloud — Sign up today!




        http://docs.engineyard.com/beta/home




                                                  36
Testing in more detail


• RSpec http://rspec.info
• Cucumber http://cukes.info
• Celerity http://celerity.rubyforge.org




                                            37
Deployment strategies


•  Glassfish http://glassfish.java.net/ (.WAR)
•  Tomcat http://tomcat.apache.org/ (.WAR)
•  JBoss http://jboss.org/ (.WAR)
•  Jetty http://jetty.codehaus.org/jetty/ (.WAR)
•  Glassfish gem http://rubyforge.org/projects/glassfishgem/ (embedded
   glassfish)
•  Trinidad https://github.com/calavera/trinidad (embedded Tomcat)
•  Torquebox http://torquebox.org/ (embedded JBoss with extras)
•  Google App Engine http://code.google.com/appengine/ (via google-
   appengine gem http://code.google.com/p/appengine-jruby/)
•  Mongrel (outdated)




                                                                        38
Further information


• The Ruby language web site http://ruby-lang.org
• Ruby On Rails web site http://rubyonrails.org
• JRuby web site http://jruby.org
• Engine Yard web site http://engineyard.com
• Using JRuby (Pragmatic Bookshelf)
  http://www.pragprog.com/titles/jruby/using-
  jruby
• Meet Rails 3 (Peep Code)
  https://peepcode.com/products/meet-rails-3-i



                                                    39
Zero to Rails 3


• http://www.engineyard.com/sparkle
• 4-day online training (December 20-23)




                                           40
http://www.flickr.com/photos/10485077@N06/4975546097
                                                  


                                                  41
Thank you!
           



    Contact me
             
hasari@engineyard.com   
  Twitter: @hiro_asari
   Github: BanzaiMan



                            http://www.flickr.com/photos/42033648@N00/372887164
                                                                             

                                                                      42

JRuby, Ruby, Rails and You on the Cloud

  • 1.
    JRuby, Ruby, Railsand You on the Cloud December 6th, 2010
  • 2.
    Who am I? HiroAsari • JRuby Support Engineer at Engine Yard • hasari@engineyard.com 2
  • 3.
    Engine Yard • Platform asa Service provider • Ruby on Rails specialists • http://www.engineyard.com 3
  • 4.
  • 5.
    WHAT IS RUBYON RAILS?" AND" WHY RUBY ON RAILS? http://www.flickr.com/photos/88019192@N00/3277925689/ 5
  • 6.
    JVM Web FrameworksSurveys • http://bit.ly/jvm-frameworks-matrix • http://bit.ly/webmatrixsurveyresults 6
  • 7.
    Rails ü Is a web application framework written in Ruby http://www.flickr.com/photos/46799485@N00/4386602878 7
  • 8.
    Ruby quick facts • Conceivedby Yukihiro Matsumoto (a.k.a. Matz) in 1993 • Object-oriented dynamically-typed interpreted scripting language • Influenced by Perl, Smalltalk, Eiffel and LISP • External libraries are most frequently distributed as “gems” 8
  • 9.
    Rails quick facts • Conceivedby David Heinemeier Hansson (a.k.a. DHH) in 2004 • Extracted from 37signals’ “Basecamp” application • Uses MVC design pattern • Rails 3, released in August, 2010, uses “Bundler” to manage gems. 9
  • 10.
  • 11.
  • 12.
  • 13.
    NetBeans (if youhave it already) 13
  • 14.
  • 15.
    RVM (Ruby VersionManager) • http://rvm.beginrescueend.com/ • rvm install jruby • rvm use jruby 15
  • 16.
    Rails ü Gets you off and running quickly 16
  • 17.
  • 18.
    jruby -S railsnew cloudstock_demo cd cloudstock_demo vi Gemfile jruby -S bundle install jruby -S bundle exec glassfish 18
  • 19.
    $ jruby -Srails new cloudstock_demo -m http://jruby.org create create README create app create app/controllers/application_controller.rb create app/helpers/application_helper.rb create app/mailers create app/models create app/views/layouts/application.html.erb create public create public/404.html create script/rails create test create test/fixtures create test/test_helper.rb create test/unit apply http://jruby.org apply http://jruby.org/templates/default.rb gsub Gemfile 19
  • 20.
  • 21.
    $ jruby script/railsgenerate scaffold user name:string calling init (1017e6ac0) invoke active_record create db/migrate/20101115150436_create_users.rb create app/models/user.rb invoke test_unit create test/unit/user_test.rb create test/fixtures/users.yml route resources :users invoke scaffold_controller create app/controllers/users_controller.rb invoke erb create app/views/users create app/views/users/index.html.erb create app/views/users/edit.html.erb create app/views/users/show.html.erb create app/views/users/new.html.erb create app/views/users/_form.html.erb invoke test_unit create test/functional/users_controller_test.rb invoke helper create app/helpers/users_helper.rb invoke test_unit create test/unit/helpers/users_helper_test.rb invoke stylesheets create public/stylesheets/scaffold.css 21
  • 22.
    $ cat db/migrate/20101115150436_create_users.rb classCreateUsers < ActiveRecord::Migration def self.up create_table :users do |t| t.string :name t.timestamps end end def self.down drop_table :users end end 22
  • 23.
    Rails ü Is descriptive 23
  • 24.
    class User <ActiveRecord::Base validates_length_of :name, :minimum => 5 end 24
  • 25.
    Rails ü Is tested 25
  • 26.
  • 27.
    $ cat test/unit/user_test.rb require'test_helper' class UserTest < ActiveSupport::TestCase test "bare user is invalid" do u = User.new assert !u.valid? assert u.errors[:name].any? end test "user with name shorter than 5 character is invalid" do u = User.new(:name => "hi") assert !u.valid? assert u.errors[:name].any? end test "user with name with at least 5 character is valid" do u = User.new(:name => "hello") assert u.valid? end end 27
  • 28.
    Rails ü Is good for agile development 28
  • 29.
    Rails ü Makes you happy 29
  • 30.
  • 31.
    Accessing Java libraryfrom Rails • Generating SVG with Batik • http://xmlgraphics.apache.org/batik/ 31
  • 32.
    Example 1: GeneratingSVG with Batik require 'java' require 'batik' java_import org.apache.batik.svggen.SVGGraphics2D java_import org.apache.batik.dom.GenericDOMImplementation dom = GenericDOMImplementation.getDOMImplementation svg_ns = 'http://www.w3.org/2000/svg' doc = dom.createDocument svg_ns, 'svg', nil svg_gen = SVGGraphics2D.new( doc ) svg_gen.set_paint java.awt.Color.send(color) svg_gen.fill java.awt.Rectangle.new(10,10,100,100) svg_gen.set_paint java.awt.Color.send(text_color) svg_gen.draw_string @user.name, 25, 55 out = java.io.StringWriter.new svg_gen.stream(out, true) render :inline => out.to_string 32
  • 33.
    Accessing Java libraryfrom Rails • Generating PDF with iText • http://itextpdf.com/ 33
  • 34.
    Example 2: GeneratingPDF with iText require 'iText-5.0.5' pdf = com.itextpdf.text.Document.new para = com.itextpdf.text.Paragraph.new "Hello #{@user.name}" file = "#{::Rails.root.to_s}/tmp/pdfs/pdf_demo.pdf” out = java.io.FileOutputStream.new file com.itextpdf.text.pdf.PdfWriter.get_instance pdf, out pdf.open pdf.add para pdf.close render :file => file Don’t do this in production code! 34
  • 35.
    JRuby on EngineYard AppCloud 35
  • 36.
    JRuby on EngineYard AppCloud — Sign up today! http://docs.engineyard.com/beta/home 36
  • 37.
    Testing in moredetail • RSpec http://rspec.info • Cucumber http://cukes.info • Celerity http://celerity.rubyforge.org 37
  • 38.
    Deployment strategies •  Glassfishhttp://glassfish.java.net/ (.WAR) •  Tomcat http://tomcat.apache.org/ (.WAR) •  JBoss http://jboss.org/ (.WAR) •  Jetty http://jetty.codehaus.org/jetty/ (.WAR) •  Glassfish gem http://rubyforge.org/projects/glassfishgem/ (embedded glassfish) •  Trinidad https://github.com/calavera/trinidad (embedded Tomcat) •  Torquebox http://torquebox.org/ (embedded JBoss with extras) •  Google App Engine http://code.google.com/appengine/ (via google- appengine gem http://code.google.com/p/appengine-jruby/) •  Mongrel (outdated) 38
  • 39.
    Further information • The Rubylanguage web site http://ruby-lang.org • Ruby On Rails web site http://rubyonrails.org • JRuby web site http://jruby.org • Engine Yard web site http://engineyard.com • Using JRuby (Pragmatic Bookshelf) http://www.pragprog.com/titles/jruby/using- jruby • Meet Rails 3 (Peep Code) https://peepcode.com/products/meet-rails-3-i 39
  • 40.
    Zero to Rails3 • http://www.engineyard.com/sparkle • 4-day online training (December 20-23) 40
  • 41.
  • 42.
    Thank you! Contact me hasari@engineyard.com Twitter: @hiro_asari Github: BanzaiMan http://www.flickr.com/photos/42033648@N00/372887164 42

Editor's Notes

  • #4 Once upon a time, there was a company which declared that “The Network Is The Computer”.These days, it is impossible to think of a computing environment without the network.
  • #6 At Devoxx 2010, Matt Raible surveyed the current status of JVM Web application frameworks.Ruby on Rails (via JRuby) came in #3 of 13 surveyed.Factors considered:Developer ProductivityDeveloper PerceptionLearning CurveProject HealthDeveloper AvailabilityJob TrendsTemplatingComponentsAjaxPlugins or Add-OnsScalabilityTestingi18n and l10nValidationMulti-language Support (Groovy / Scala)Quality of Documentation/TutorialsBooks PublishedREST Support (client and server)Mobile / iPhone SupportDegree of RiskAfollowup survey by another http://sebastien-arbogast.com/2010/11/19/jvm-web-framework-survey-first-results/From a small sample size, Rails came out #1.But really, you need to see for yourself if Rails (or any other Web application framework) works for YOU!
  • #7 “Convention over configuration”
  • #8 The original implementation of Ruby (also called CRuby, or MRI—Matz’s Ruby Interpreter) is implemented.A newer implemenation of the language, such as JRuby and Rubinius, can compile Ruby programs into byte code to execute them.
  • #9 Blog in 15 minutes: http://www.youtube.com/watch?v=Gzj723LkRJY
  • #10 The list might be outdated.Backpack (37 Signals)Cookpad (Recipe sharing site in Japan)Get Satisfaction (Customer service helper)Github (Social code sharing)Hulu (Online video streaming)Oracle Mix (SNS for Oracle professionals)New York Jets (J. E. T. S. Jets, Jets, Jets)Pragmatic BookshelfGrouponLighthouse (Development issue tracker)ZenDesk (Customer Support ticket tracker)
  • #11 http://jruby.org/download1.5.6 was released last Friday (Dec. 3, 2010)1.6 coming later this month or early next year
  • #12 NetBeans
  • #13 You can install and activate Ruby and Rails plugin if you have NetBeans already. (6.9 and later supports Rails 3.)
  • #14 RubyMine
  • #16 “Convention over configuration”In web application development, there are a lot of things that happen over and over.(Setting up MVC, configuring Database connections, laying out directory structures, etc.) Rails aims to make sensible decisions for you, so that you don’t have to make these decisions.You might think it’s constraining at first, but you will soon learn that by allowing you to spend more time thinking about the application itself (rather than “administrivia”) Rails liberates you.
  • #17 These are the commands used in the previous video.
  • #18 It creates a lot of directories and files with boilerplate content.Give -d mysql to use MySQL as the database engine.
  • #19 It creates a lot of directories and files with boilerplate content.
  • #20 Database schema modification is described in Ruby as well.This migration file was generated by the scaffold generator.
  • #21 Rails can be described as a DSL for web application development.
  • #22 It is clear what this piece of code does.
  • #23 Testing is a part of Ruby and Rails culture.
  • #24 Scaffold generator that we ran earlier also created a few files for tests as well.
  • #25 You *could* add test code such as this.You won’t have to test “validates_length_of” in this way, since it is a part of Rails, and, as such, it is very well tested and understood.
  • #26 Here, “agile” means short feedback cycles
  • #27 The bottom line is that Rails makes you happy.
  • #28 JRuby is an implementation of the Ruby language on the JVM. It can interoperate with the vast collection of existing Java libraries.http://vimeo.com/16270284 http://trackernet.org
  • #29 *DEMO 1*
  • #30 This example generates a simple SVG graphics with Batik library. http://xmlgraphics.apache.org/batik/
  • #31 *DEMO 2*
  • #32 This example generates a PDF file through iText. http://itextpdf.com/This is for illustration purposes only. It is not very secure, not is it very scalable.
  • #34 If you’re interested, sign up to be notified.
  • #35 What we did not cover today
  • #36 What we did not cover today:WAR files are created through warbler gem. http://kenai.com/projects/warbler/pages/HomeTorquebox’s (http://torquebox.org) extras include:ActiveMQ and built-in job scheduling supporthttp://jruby-appengine.blogspot.com/
  • #38 Engine Yard is offering an online training course of Rails 3 later this month.
  • #39 Go grab a piece of Cloud.