SlideShare uma empresa Scribd logo
1 de 43
Baixar para ler offline
(track sponsor)




                         Jruby &
                        Iron Ruby

                        Amir Barylko


Monday, June 13, 2011
AMIR BARYLKO
                        JRUBY & IRON RUBY

                                   PRAIRIE DEV CON
                                       JUN 2011




Amir Barylko - Jruby & Iron Ruby                     MavenThought Inc.
Monday, June 13, 2011
WHO AM I?

   • Architect

   • Developer

   • Mentor

   • Great       cook

   • The      one who’s entertaining you for the next while!



Amir Barylko - Jruby & Iron Ruby                               MavenThought Inc.
Monday, June 13, 2011
CONTACT AND MATERIALS
   • Contact            me: amir@barylko.com, @abarylko

   • Blog:http://www.orthocoders.com

   • Site:http://www.maventhought.com

   • Download: http://www.orthocoders.com/presentations.




Amir Barylko - Jruby & Iron Ruby                           MavenThought Inc.
Monday, June 13, 2011
RUBY INTRO
                                    Dynamic languages
                                        Toolbox
                                        Features




Amir Barylko - Jruby & Iron Ruby                        MavenThought Inc.
Monday, June 13, 2011
DYNAMIC LANGUAGES

          High level
          Dynamically typed
          Runtime over compile time
          Closures
          Reflection
          Platform independent

Amir Barylko - Jruby & Iron Ruby       MavenThought Inc.
Monday, June 13, 2011
DEVELOPERS TOOLBOX
   • Make        your toolbox grow!

   • The      right tool for the job

   • Not      a replacement

   • Combine            strengths

   • Problem            solving



Amir Barylko - Jruby & Iron Ruby       MavenThought Inc.
Monday, June 13, 2011
WELCOME TO RUBY

          Created in mid-90s by          Several implementations:
           “Matz” Matsumoto in Japan       MRI, YARB, JRuby
          Smalltalk, Perl influences      Totally free!!
          Dynamic typing
          Object Oriented
          Automatic memory
           management

Amir Barylko - Jruby & Iron Ruby                              MavenThought Inc.
Monday, June 13, 2011
RUBY FEATURES

          Everything is an expression      Operator overloading,
                                             flexible syntax
          Metaprogramming
                                            Powerful standard library
          Closures
          Garbage collection
          Exceptions



Amir Barylko - Jruby & Iron Ruby                                MavenThought Inc.
Monday, June 13, 2011
RUBY SUPPORT

          Hundreds of books                Lots of great web sites:
                                             basecamp, twitter, 43
          User conferences all over         things, hulu, scribd,
           the world                         slideshare, Justin.tv
          Active community (you can        Lots of web frameworks
           create a conference in your       inspired by Ruby on Rails
           own city and top Ruby
           coders will go there to
           teach others, invite them
           and see)

Amir Barylko - Jruby & Iron Ruby                                 MavenThought Inc.
Monday, June 13, 2011
RUBY LANGUAGE
                                      Basic Types
                                   Control Structures
                                       Methods
                                       Modifiers




Amir Barylko - Jruby & Iron Ruby                        MavenThought Inc.
Monday, June 13, 2011
BASIC TYPES
        Numbers
          1.class => Fixnum
          1.1.class => Float
          (120**100).class => Bignum
          3.times {puts “he “}




Amir Barylko - Jruby & Iron Ruby                 MavenThought Inc.
Monday, June 13, 2011
BASIC TYPES II
   • Strings

      'he ' + 'he' => he he
      “That's right” => That's right
      'He said “hi”' => He said “hi”
      “He said “hi”” => He said “hi”
      “1 + 1 is #{1+1}” => 1 + 1 is 2
      "#{'Ho! '*3}Merry Christmas" =>Ho! Ho! Ho! Merry
      Christmas



Amir Barylko - Jruby & Iron Ruby                    MavenThought Inc.
Monday, June 13, 2011
BASIC TYPES III
      Arrays
       a = [1, 5.5, “nice!”]
       1 == a.first
       1 == a[0]
       nil == a[10]
       a[1] = 3.14
       a.each {|elem| puts elem}
       a.sort



Amir Barylko - Jruby & Iron Ruby                     MavenThought Inc.
Monday, June 13, 2011
BASIC TYPES IV
   • Hashes
      h = {“one” => 1, 1 => “one”}
      h[“one”] == 1
      h[1] == “one”
      h[“two”] == nil
      h.keys == [“one”, 1] (or is it [1, “one”] ?)
      h.values == [“one”, 1] (or is it [1, “one”] ?)
      h[“one”] = 1.0



Amir Barylko - Jruby & Iron Ruby                    MavenThought Inc.
Monday, June 13, 2011
BASIC TYPES V
       Symbols: constant names. No need to declare, guaranteed
        uniqueness, fast comparison

   :apple == :apple
   :orange != :banana
   [:all, :those, :symbols]
   {:ca => “Canada”, :ar => “Argentina”, :es => “Spain”}




Amir Barylko - Jruby & Iron Ruby                            MavenThought Inc.
Monday, June 13, 2011
CONTROL STRUCTURES
          if
           if count < 20
             puts “need more”
           elsif count < 40
             puts “perfect”
           else
             puts “too many”
           end
          while
            while count < 100 && need_more
                buy(1)
            end
Amir Barylko - Jruby & Iron Ruby             MavenThought Inc.
Monday, June 13, 2011
CONTROL STRUCTURES II
        Statement modifiers
         buy while need_more?

         buy(5) if need_more?

         buy until left == 0

         buy unless left < 5




Amir Barylko - Jruby & Iron Ruby   MavenThought Inc.
Monday, June 13, 2011
CONTROL STRUCTURES III
   • Case
       case left
            when 0..5
              dont_buy_more
            when 6..10
              buy(1)
            when 10..100
              buy(5)
       end

Amir Barylko - Jruby & Iron Ruby   MavenThought Inc.
Monday, June 13, 2011
METHODS
       Simple
          def play(movie_path)
          ....
          end

       Default arguments
          def play(movie_path, auto_start = true, wrap = false)
          ....
          end




Amir Barylko - Jruby & Iron Ruby                           MavenThought Inc.
Monday, June 13, 2011
METHODS II
        Return value: the last expression evaluated, no need for
         explicit return
         def votes(voted, num_votes)
           voted && num_votes || nil
         end

        No need for parenthesis on call without arguments (same
         syntax to call a method and a field)
         buy() == buy
         movie.play() == movie.play

Amir Barylko - Jruby & Iron Ruby                                MavenThought Inc.
Monday, June 13, 2011
METHODS III
       No need also with arguments (but careful!! only if you know
        what you are doing)

         movie.play “Pulp fiction”, false, true




Amir Barylko - Jruby & Iron Ruby                              MavenThought Inc.
Monday, June 13, 2011
RUBY LANGUAGE II
                                     Classes
                                      Mixin
                                   Enumerable




Amir Barylko - Jruby & Iron Ruby                MavenThought Inc.
Monday, June 13, 2011
CLASSES & OBJECTS

       Initializer and instance variables
         class Movie
           def initialize(name)
             @name = name
           end

           def play
             puts %Q{Playing “#{@name}”. Enjoy!}
           end
         end

         m = Movie.new(“Pulp fiction”)
         m.play

         => Playing “Pulp fiction”. Enjoy!


Amir Barylko - Jruby & Iron Ruby                   MavenThought Inc.
Monday, June 13, 2011
CLASSES & OBJECTS II
       Attributes
     class Movie
       def initialize(name)
         @name = name
       end



                                                          }
         def name                  # attr_reader :name
           @name
         end                                                  # attr_accessor :name

         def name=(value)          # attr_writter :name
           @name = value
         end

     end

     m = Movie.new('Brazil').name = “Pulp fiction”



Amir Barylko - Jruby & Iron Ruby                                            MavenThought Inc.
Monday, June 13, 2011
CODE ORGANIZATION
        Code in files with .rb extension
        Require 'movie' will read movie.rb file and make its methods
         available to the current file
        Require 'media/movie' will read file from media dir relative
         to the current working dir
            $LOAD_PATH << 'media'
            require 'movie'




Amir Barylko - Jruby & Iron Ruby                               MavenThought Inc.
Monday, June 13, 2011
CODE ORGANIZATION II
       Relative to this file:
         require File.join(File.dirname(__FILE__), 'media/movie')




Amir Barylko - Jruby & Iron Ruby                           MavenThought Inc.
Monday, June 13, 2011
MIXINS
       What about module “instance methods”?
       One of the greatest Ruby features!
       You can define functions in Modules, and get them added to
        your classes.
             Great code reuse,
             Multiple inheritance alternative.
             Code organization


Amir Barylko - Jruby & Iron Ruby                            MavenThought Inc.
Monday, June 13, 2011
ENUMERABLE
        Enumerable mixin, from the standard library documentation:


           The Enumerable mixin provides collection
           classes with several traversal and
           searching methods, and with the ability to
           sort. The class must provide a method each,
           which yields successive members of the
           collection


Amir Barylko - Jruby & Iron Ruby                            MavenThought Inc.
Monday, June 13, 2011
ENUMERABLE II
       It provides useful methods such as:

             map

             to_a

             take_while

             count

             inject


Amir Barylko - Jruby & Iron Ruby               MavenThought Inc.
Monday, June 13, 2011
EXAMPLES
                                        rSpec
                                     Enumerable
                                    Method Missing
                                       Sinatra
                                    BDD Cucumber
                                         DSL

Amir Barylko - Jruby & Iron Ruby                     MavenThought Inc.
Monday, June 13, 2011
RSPEC TESTING LIBRARY
   require 'java'
   require 'rubygems'

   java_import 'MovieLibrary'
   java_import 'Movie'

   describe MovieLibrary do

     it "should be created empty" do
       lib = MovieLibrary.new
       lib.contents.should be_empty
     end

     it "should add an element" do
       lib = MovieLibrary.new
       m = Movie.new 'Blazing Saddles'
       lib.add m
       lib.contents.should include(m)
       lib.contents.count.should == 1
     end

   end



Amir Barylko - Jruby & Iron Ruby         MavenThought Inc.
Monday, June 13, 2011
ENUMERBLE MIXIN
   require 'rubygems'
   java_import 'MovieLibrary'
   java_import 'Movie'

   class MovieLibrary
     include Enumerable
     def each(&block)
       contents.each(&block)
     end
   end



Amir Barylko - Jruby & Iron Ruby          MavenThought Inc.
Monday, June 13, 2011
ENUMERBLE MIXIN
   lib = MovieLibrary.new

   lib.add(Movie.new "Blazing Saddles")
   lib.add(Movie.new "Spaceballs")
   lib.add(Movie.new "Young Frankenstein")

   lib.each { |m| puts "The movie is #{m.title}"}

   puts "nn**** Using Inject"
   lib.inject([]) { |a, m| a << m.title }
      .each { |s| puts "The movie is #{s}" }

   puts "nn*** Using find"
   lib.find_all { |m| m.title.end_with? 's' }
      .each { |m| puts "The movies that end with s: #{m.title}"}


Amir Barylko - Jruby & Iron Ruby                              MavenThought Inc.
Monday, June 13, 2011
EXTEND LIBRARY
              WITH METHOD MISSING
   require File.dirname(__FILE__) + "....bin/Debug/MavenThought.MovieLibrary.dll"
   require 'rubygems'
   include MavenThought::MovieLibrary

   class Library
     def method_missing(m, *args)
       if m.id2name.include?("find_by")
         field = m.id2name.sub( /find_by_/, "")
         contents.find_all( lambda { |m| m.send(field) == args[0] } )
       else
         super
       end
     end
   end


   lib = Library.new

   lib.add Movie.new( "Blazing Saddles", System::DateTime.new(1972, 1, 1))
   lib.add Movie.new( "Spaceballs", System::DateTime.new(1987, 1, 1))

   puts "Using find by title #{lib.find_by_title( "Blazing Saddles" )}"

Amir Barylko - Jruby & Iron Ruby                                        MavenThought Inc.
Monday, June 13, 2011
SIMPLE WEB WITH SINATRA
   require   'rubygems'
   require   'sinatra'
   require   'haml'
   require   'singleton'

   java_import 'MovieLibrary'
   java_import 'Movie'

   class MovieLibrary
     include Singleton
   end

   # index
   get '/' do
     @movies = MovieLibrary.instance.contents
     haml :index
   end

   # create
   post '/' do
     m = Movie.new(params[:title])
     MovieLibrary.instance.add m
     redirect '/'
   end



Amir Barylko - Jruby & Iron Ruby                MavenThought Inc.
Monday, June 13, 2011
BDD WITH CUCUMBER
   Feature: Addition
     In order to make my library grow
     As a registered user
     I want to add movies to the library

      Scenario: Add a movie
        Given I have an empty library
        When I add the following movies:
             | title              | release_date |
             | Blazing Saddles    | Feb 7, 1974 |
             | Young Frankenstein | Dec 15, 1974 |
             | Spaceballs         | Jun 24, 1987 |
        Then The library should have 3 movies
        And "Blazing Saddles" should be in the list with date "Feb 7, 1974"
        And "Young Frankenstein" should be in the list with date "Dec 15, 1974"
        And "Spaceballs" should be in the list with release date "Jun 24, 1987"




Amir Barylko - Jruby & Iron Ruby                                     MavenThought Inc.
Monday, June 13, 2011
BDD WITH CUCUMBER
   require File.dirname(__FILE__) + "...MavenThought.MovieLibrary.dll"

   include MavenThought::MovieLibrary

   Given /^I have an empty library$/ do
     @lib = Library.new
   end

   When /^I add the following movies:$/ do |table|
     table.hashes.each do |row|
       movie = Movie.new row[:title], System::DateTime.parse(...)
       @lib.add movie
     end
   end




Amir Barylko - Jruby & Iron Ruby                              MavenThought Inc.
Monday, June 13, 2011
DSL I
                                    RAKE
   desc "Builds the project"
   task :build do
   	

 call_target msbuild_cmd, :build
   end

   desc "Rebuild the application by cleaning and then building"
   task :rebuild => [:clean, :build]

   desc "Runs all the tests"
   task :test => ["test:all"]


Amir Barylko - Jruby & Iron Ruby                                  MavenThought Inc.
Monday, June 13, 2011
DSL II
                        CRON - WHENEVER
    every 10.minutes do
       runner "MyModel.some_process"
       rake "my:rake:task"
       command "/usr/bin/my_great_command"
    end


    every 2.days, :at => '4:30am' do
       command "/usr/bin/my_great_command"
    end




Amir Barylko - Jruby & Iron Ruby             MavenThought Inc.
Monday, June 13, 2011
QUESTIONS?




                          (Don’t be shy)




Monday, June 13, 2011
CONTACT AND MATERIALS
   • Contact            me: amir@barylko.com, @abarylko

   • Blog:http://www.orthocoders.com

   • Site:http://www.maventhought.com

   • Download: http://www.orthocoders.com/presentations.




Amir Barylko - Jruby & Iron Ruby                           MavenThought Inc.
Monday, June 13, 2011
ONLINE RESOURCES
       IronRuby: http://ironruby.net/
       Jruby: http://jruby.org
       Ruby language site: http://www.ruby-lang.org
       rSpec: http://rspec.info/
       Sinatra: http://www.sinatrarb.com/
       Cucumber: http://cukes.info/
       Rake: http://rake.rubyforge.org/
Amir Barylko - Jruby & Iron Ruby                       MavenThought Inc.
Monday, June 13, 2011

Mais conteúdo relacionado

Mais de Amir Barylko

Beutiful javascript with coffeescript
Beutiful javascript with coffeescriptBeutiful javascript with coffeescript
Beutiful javascript with coffeescript
Amir Barylko
 
Rich UI with Knockout.js &amp; Coffeescript
Rich UI with Knockout.js &amp; CoffeescriptRich UI with Knockout.js &amp; Coffeescript
Rich UI with Knockout.js &amp; Coffeescript
Amir Barylko
 

Mais de Amir Barylko (20)

Functional converter project
Functional converter projectFunctional converter project
Functional converter project
 
Elm: delightful web development
Elm: delightful web developmentElm: delightful web development
Elm: delightful web development
 
Dot Net Core
Dot Net CoreDot Net Core
Dot Net Core
 
No estimates
No estimatesNo estimates
No estimates
 
User stories deep dive
User stories deep diveUser stories deep dive
User stories deep dive
 
Coderetreat hosting training
Coderetreat hosting trainingCoderetreat hosting training
Coderetreat hosting training
 
There's no charge for (functional) awesomeness
There's no charge for (functional) awesomenessThere's no charge for (functional) awesomeness
There's no charge for (functional) awesomeness
 
What's new in c# 6
What's new in c# 6What's new in c# 6
What's new in c# 6
 
Productive teams
Productive teamsProductive teams
Productive teams
 
Who killed object oriented design?
Who killed object oriented design?Who killed object oriented design?
Who killed object oriented design?
 
From coach to owner - What I learned from the other side
From coach to owner - What I learned from the other sideFrom coach to owner - What I learned from the other side
From coach to owner - What I learned from the other side
 
Communication is the Key to Teamwork and productivity
Communication is the Key to Teamwork and productivityCommunication is the Key to Teamwork and productivity
Communication is the Key to Teamwork and productivity
 
Acceptance Test Driven Development
Acceptance Test Driven DevelopmentAcceptance Test Driven Development
Acceptance Test Driven Development
 
Refactoring
RefactoringRefactoring
Refactoring
 
Agile requirements
Agile requirementsAgile requirements
Agile requirements
 
Agile teams and responsibilities
Agile teams and responsibilitiesAgile teams and responsibilities
Agile teams and responsibilities
 
Refactoring
RefactoringRefactoring
Refactoring
 
Beutiful javascript with coffeescript
Beutiful javascript with coffeescriptBeutiful javascript with coffeescript
Beutiful javascript with coffeescript
 
Sass & bootstrap
Sass & bootstrapSass & bootstrap
Sass & bootstrap
 
Rich UI with Knockout.js &amp; Coffeescript
Rich UI with Knockout.js &amp; CoffeescriptRich UI with Knockout.js &amp; Coffeescript
Rich UI with Knockout.js &amp; Coffeescript
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

PRDC11-Jruby-ironruby

  • 1. (track sponsor) Jruby & Iron Ruby Amir Barylko Monday, June 13, 2011
  • 2. AMIR BARYLKO JRUBY & IRON RUBY PRAIRIE DEV CON JUN 2011 Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 3. WHO AM I? • Architect • Developer • Mentor • Great cook • The one who’s entertaining you for the next while! Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 4. CONTACT AND MATERIALS • Contact me: amir@barylko.com, @abarylko • Blog:http://www.orthocoders.com • Site:http://www.maventhought.com • Download: http://www.orthocoders.com/presentations. Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 5. RUBY INTRO Dynamic languages Toolbox Features Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 6. DYNAMIC LANGUAGES  High level  Dynamically typed  Runtime over compile time  Closures  Reflection  Platform independent Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 7. DEVELOPERS TOOLBOX • Make your toolbox grow! • The right tool for the job • Not a replacement • Combine strengths • Problem solving Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 8. WELCOME TO RUBY  Created in mid-90s by  Several implementations: “Matz” Matsumoto in Japan MRI, YARB, JRuby  Smalltalk, Perl influences  Totally free!!  Dynamic typing  Object Oriented  Automatic memory management Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 9. RUBY FEATURES  Everything is an expression  Operator overloading, flexible syntax  Metaprogramming  Powerful standard library  Closures  Garbage collection  Exceptions Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 10. RUBY SUPPORT  Hundreds of books  Lots of great web sites: basecamp, twitter, 43  User conferences all over things, hulu, scribd, the world slideshare, Justin.tv  Active community (you can  Lots of web frameworks create a conference in your inspired by Ruby on Rails own city and top Ruby coders will go there to teach others, invite them and see) Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 11. RUBY LANGUAGE Basic Types Control Structures Methods Modifiers Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 12. BASIC TYPES  Numbers 1.class => Fixnum 1.1.class => Float (120**100).class => Bignum 3.times {puts “he “} Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 13. BASIC TYPES II • Strings 'he ' + 'he' => he he “That's right” => That's right 'He said “hi”' => He said “hi” “He said “hi”” => He said “hi” “1 + 1 is #{1+1}” => 1 + 1 is 2 "#{'Ho! '*3}Merry Christmas" =>Ho! Ho! Ho! Merry Christmas Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 14. BASIC TYPES III  Arrays a = [1, 5.5, “nice!”] 1 == a.first 1 == a[0] nil == a[10] a[1] = 3.14 a.each {|elem| puts elem} a.sort Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 15. BASIC TYPES IV • Hashes h = {“one” => 1, 1 => “one”} h[“one”] == 1 h[1] == “one” h[“two”] == nil h.keys == [“one”, 1] (or is it [1, “one”] ?) h.values == [“one”, 1] (or is it [1, “one”] ?) h[“one”] = 1.0 Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 16. BASIC TYPES V  Symbols: constant names. No need to declare, guaranteed uniqueness, fast comparison :apple == :apple :orange != :banana [:all, :those, :symbols] {:ca => “Canada”, :ar => “Argentina”, :es => “Spain”} Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 17. CONTROL STRUCTURES  if if count < 20 puts “need more” elsif count < 40 puts “perfect” else puts “too many” end  while while count < 100 && need_more buy(1) end Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 18. CONTROL STRUCTURES II  Statement modifiers buy while need_more? buy(5) if need_more? buy until left == 0 buy unless left < 5 Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 19. CONTROL STRUCTURES III • Case case left when 0..5 dont_buy_more when 6..10 buy(1) when 10..100 buy(5) end Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 20. METHODS  Simple def play(movie_path) .... end  Default arguments def play(movie_path, auto_start = true, wrap = false) .... end Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 21. METHODS II  Return value: the last expression evaluated, no need for explicit return def votes(voted, num_votes) voted && num_votes || nil end  No need for parenthesis on call without arguments (same syntax to call a method and a field) buy() == buy movie.play() == movie.play Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 22. METHODS III  No need also with arguments (but careful!! only if you know what you are doing) movie.play “Pulp fiction”, false, true Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 23. RUBY LANGUAGE II Classes Mixin Enumerable Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 24. CLASSES & OBJECTS  Initializer and instance variables class Movie def initialize(name) @name = name end def play puts %Q{Playing “#{@name}”. Enjoy!} end end m = Movie.new(“Pulp fiction”) m.play => Playing “Pulp fiction”. Enjoy! Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 25. CLASSES & OBJECTS II  Attributes class Movie def initialize(name) @name = name end } def name # attr_reader :name @name end # attr_accessor :name def name=(value) # attr_writter :name @name = value end end m = Movie.new('Brazil').name = “Pulp fiction” Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 26. CODE ORGANIZATION  Code in files with .rb extension  Require 'movie' will read movie.rb file and make its methods available to the current file  Require 'media/movie' will read file from media dir relative to the current working dir $LOAD_PATH << 'media' require 'movie' Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 27. CODE ORGANIZATION II  Relative to this file: require File.join(File.dirname(__FILE__), 'media/movie') Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 28. MIXINS  What about module “instance methods”?  One of the greatest Ruby features!  You can define functions in Modules, and get them added to your classes.  Great code reuse,  Multiple inheritance alternative.  Code organization Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 29. ENUMERABLE  Enumerable mixin, from the standard library documentation: The Enumerable mixin provides collection classes with several traversal and searching methods, and with the ability to sort. The class must provide a method each, which yields successive members of the collection Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 30. ENUMERABLE II  It provides useful methods such as:  map  to_a  take_while  count  inject Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 31. EXAMPLES rSpec Enumerable Method Missing Sinatra BDD Cucumber DSL Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 32. RSPEC TESTING LIBRARY require 'java' require 'rubygems' java_import 'MovieLibrary' java_import 'Movie' describe MovieLibrary do it "should be created empty" do lib = MovieLibrary.new lib.contents.should be_empty end it "should add an element" do lib = MovieLibrary.new m = Movie.new 'Blazing Saddles' lib.add m lib.contents.should include(m) lib.contents.count.should == 1 end end Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 33. ENUMERBLE MIXIN require 'rubygems' java_import 'MovieLibrary' java_import 'Movie' class MovieLibrary include Enumerable def each(&block) contents.each(&block) end end Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 34. ENUMERBLE MIXIN lib = MovieLibrary.new lib.add(Movie.new "Blazing Saddles") lib.add(Movie.new "Spaceballs") lib.add(Movie.new "Young Frankenstein") lib.each { |m| puts "The movie is #{m.title}"} puts "nn**** Using Inject" lib.inject([]) { |a, m| a << m.title } .each { |s| puts "The movie is #{s}" } puts "nn*** Using find" lib.find_all { |m| m.title.end_with? 's' } .each { |m| puts "The movies that end with s: #{m.title}"} Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 35. EXTEND LIBRARY WITH METHOD MISSING require File.dirname(__FILE__) + "....bin/Debug/MavenThought.MovieLibrary.dll" require 'rubygems' include MavenThought::MovieLibrary class Library def method_missing(m, *args) if m.id2name.include?("find_by") field = m.id2name.sub( /find_by_/, "") contents.find_all( lambda { |m| m.send(field) == args[0] } ) else super end end end lib = Library.new lib.add Movie.new( "Blazing Saddles", System::DateTime.new(1972, 1, 1)) lib.add Movie.new( "Spaceballs", System::DateTime.new(1987, 1, 1)) puts "Using find by title #{lib.find_by_title( "Blazing Saddles" )}" Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 36. SIMPLE WEB WITH SINATRA require 'rubygems' require 'sinatra' require 'haml' require 'singleton' java_import 'MovieLibrary' java_import 'Movie' class MovieLibrary include Singleton end # index get '/' do @movies = MovieLibrary.instance.contents haml :index end # create post '/' do m = Movie.new(params[:title]) MovieLibrary.instance.add m redirect '/' end Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 37. BDD WITH CUCUMBER Feature: Addition In order to make my library grow As a registered user I want to add movies to the library Scenario: Add a movie Given I have an empty library When I add the following movies: | title | release_date | | Blazing Saddles | Feb 7, 1974 | | Young Frankenstein | Dec 15, 1974 | | Spaceballs | Jun 24, 1987 | Then The library should have 3 movies And "Blazing Saddles" should be in the list with date "Feb 7, 1974" And "Young Frankenstein" should be in the list with date "Dec 15, 1974" And "Spaceballs" should be in the list with release date "Jun 24, 1987" Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 38. BDD WITH CUCUMBER require File.dirname(__FILE__) + "...MavenThought.MovieLibrary.dll" include MavenThought::MovieLibrary Given /^I have an empty library$/ do @lib = Library.new end When /^I add the following movies:$/ do |table| table.hashes.each do |row| movie = Movie.new row[:title], System::DateTime.parse(...) @lib.add movie end end Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 39. DSL I RAKE desc "Builds the project" task :build do call_target msbuild_cmd, :build end desc "Rebuild the application by cleaning and then building" task :rebuild => [:clean, :build] desc "Runs all the tests" task :test => ["test:all"] Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 40. DSL II CRON - WHENEVER every 10.minutes do runner "MyModel.some_process" rake "my:rake:task" command "/usr/bin/my_great_command" end every 2.days, :at => '4:30am' do command "/usr/bin/my_great_command" end Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 41. QUESTIONS? (Don’t be shy) Monday, June 13, 2011
  • 42. CONTACT AND MATERIALS • Contact me: amir@barylko.com, @abarylko • Blog:http://www.orthocoders.com • Site:http://www.maventhought.com • Download: http://www.orthocoders.com/presentations. Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011
  • 43. ONLINE RESOURCES  IronRuby: http://ironruby.net/  Jruby: http://jruby.org  Ruby language site: http://www.ruby-lang.org  rSpec: http://rspec.info/  Sinatra: http://www.sinatrarb.com/  Cucumber: http://cukes.info/  Rake: http://rake.rubyforge.org/ Amir Barylko - Jruby & Iron Ruby MavenThought Inc. Monday, June 13, 2011