SlideShare a Scribd company logo
1 of 37
Download to read offline
A (Quick) Introduction
      to JRuby
          Frederic Jean
       fred@fredjean.net
Hello JRuby
Develop, Deploy, Build, Test, Monitor
                                                  Configuration
                              DSLs
                                                  Glue, Wiring
External System Integration




                                      Dynamic Layer
                                     Application Code




                                     Stable Layer
                                 Java, Statically typed
                                   Legacy libraries
Ruby
Ruby is...

• Dynamically Typed
• Strongly Typed
• Everything is An Object
(Almost) Everything is
     an Object
Ruby Literals
# Strings
quot;This is a Stringquot;
'This is also a String'
%{So is this}

# Numbers
1, 0xa2, 0644, 3.14, 2.2e10

# Arrays and Hashes
['John', 'Henry', 'Mark']
{ :hello => quot;bonjourquot;, :bye => quot;au revoirquot;}

# Regular Expressions
quot;Mary had a little lambquot; =~ /little/
quot;The London Bridgequot; =~ %r{london}i

# Ranges
(1..100) # inclusive
(1...100) # exclusive
Symbols

              :symbol


• String-like construct
• Only one copy in memory
Blocks

['John', 'Henry', 'Mark'].each { |name| puts quot;#{name}quot;}

File.open(quot;/tmp/password.txtquot;) do |input|
  text = input.read
end
Ruby Classes

class Person
  attr_accessor :name, :title
end

p = Person.new
Open Classes
      class Fixnum
        def odd?
          self % 2 == 1
        end

        def even?
          self % 2 == 0
        end
      end


http://localhost:4567/code/fixnum.rb
Mixins
     module Parity
       def even?
         self % 2 == 0
       end

       def odd?
         self % 2 == 1
       end
     end


http://localhost:4567/code/parity.rb
Mixins
   class Person
     include Enumerable

     attr_accessor :first_name, :last_name

     def initialize(first_name, last_name)
       @first_name = first_name
       @last_name = last_name
     end

     def <=>(other)
       if self.last_name == other.last_name
         self.first_name <=> other.first_name
       else
         self.last_name <=> other.last_name
       end
     end

     def to_s
       quot;#{first_name} #{last_name}quot;
     end
   end

http://localhost:4567/code/sorting.rb
JRuby
JRuby

• Ruby Implementation on the JVM
• Compatible with Ruby 1.8.6
• Native Multithreading
• Full Access to Java Libraries
JRuby Hello World


java.lang.System.out.println quot;Hello JRuby!quot;




     http://localhost:4567/code/jruby_hello.rb
Java Integration


 require 'java'
JI: Ruby-like Methods

           Java                        Ruby
Locale.US                    Locale::US
System.currentTimeMillis()   System.current_time_millis
locale.getLanguage()         locale.language
date.getTime()               date.time
date.setTime(10)             date.time = 10
file.isDirectory()           file.directory?
JI: Java Extensions

      h = java.util.HashMap.new
      h[quot;keyquot;] = quot;valuequot;
      h[quot;keyquot;]
      # => quot;valuequot;
      h.get(quot;keyquot;)
      # => quot;valuequot;
      h.each {|k,v| puts k + ' => ' + v}
      # key => value
      h.to_a
      # => [[quot;keyquot;, quot;valuequot;]]




http://localhost:4567/code/ji/extensions.rb
JI: Java Extensions
module java::util::Map
  include Enumerable

  def each(&block)
    entrySet.each { |pair| block.call([pair.key, pair.value]) }
  end

  def [](key)
    get(key)
  end

  def []=(key,val)
    put(key,val)
    val
  end
end
JI: Open Java Classes
class Java::JavaLang::Integer
  def even?
    int_value % 2 == 0
  end

  def odd?
    int_value % 2 == 1
  end
end


 http://localhost:4567/code/ji/integer.rb
JI: Interface Conversion

 package java.util.concurrent;

 public class Executors {
    // ...
    public static Callable callable(Runnable r) {
       // ...
    }
 }
JI: Interface Conversion

class SimpleRubyObject
  def run
    puts quot;hiquot;
  end
end

callable = Executors.callable(SimpleRubyObject.new)
callable.call




http://localhost:4567/code/ji/if_conversion.rb
JI: Closure Conversion


callable = Executors.callable { puts quot;hiquot; }
callable.call




  http://localhost:4567/code/ji/closure_conversion.rb
Develop, Deploy, Build, Test, Monitor
                                                  Configuration
                              DSLs
                                                  Glue, Wiring
External System Integration




                                      Dynamic Layer
                                     Application Code




                                     Stable Layer
                                 Java, Statically typed
                                   Legacy libraries
Usage

• Runtime
• Wrapper around Java Code
• Calling From Java
• Utilities
JRuby as a Runtime
JRuby on Rails

• Wrap Rails app into a war file
• Direct support in Glassfish v3 Prelude
• http://kenai.com
• http://mediacast.sun.com
webmate

require 'rubygems'
require 'sinatra'

get '/*' do
  file, line = request.path_info.split(/:/)
  local_file = File.join(Dir.pwd, file)
  if (File.exists?(local_file))
    redirect quot;txmt://open/?url=file://#{local_file}&line=#{line}quot;
  else
    not_found
  end
end




     http://localhost:4567/code/webmate.rb
Wrapping Java Code
Swing Applications

frame = JFrame.new(quot;Hello Swingquot;)


   • Reduce verbosity
   • Simplify event handlers
       http://localhost:4567/code/swing.rb
RSpec Testing Java
 describe quot;An emptyquot;, HashMap do
   before :each do
     @hash_map = HashMap.new
   end

   it quot;should be able to add an entry to itquot; do
     @hash_map.put quot;fooquot;, quot;barquot;
     @hash_map.get(quot;fooquot;).should == quot;barquot;
   end
 end


 JTestr Provides Ant and Maven integration
          http://jtestr.codehaus.org

http://localhost:4567/code/hashmap_spec.rb
Utilities
Develop, Deploy, Build, Test, Monitor
                                                  Configuration
                              DSLs
                                                  Glue, Wiring
External System Integration




                                      Dynamic Layer
                                     Application Code




                                     Stable Layer
                                 Java, Statically typed
                                   Legacy libraries
Build Utilities

• Rake
• Raven
• Buildr
Conclusion
Resources
• http://jruby.org
• http://wiki.jruby.org
• http://jruby.kenai.com
• http://jtester.codehaus.org
• http://raven.rubyforge.org
• http://buildr.apache.org

More Related Content

What's hot

javascript teach
javascript teachjavascript teach
javascript teachguest3732fa
 
Open Source Compiler Construction for the JVM
Open Source Compiler Construction for the JVMOpen Source Compiler Construction for the JVM
Open Source Compiler Construction for the JVMTom Lee
 
JRuby @ Boulder Ruby
JRuby @ Boulder RubyJRuby @ Boulder Ruby
JRuby @ Boulder RubyNick Sieger
 
Going to Mars with Groovy Domain-Specific Languages
Going to Mars with Groovy Domain-Specific LanguagesGoing to Mars with Groovy Domain-Specific Languages
Going to Mars with Groovy Domain-Specific LanguagesGuillaume Laforge
 
Connecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyConnecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyNick Sieger
 
Getting started with Perl XS and Inline::C
Getting started with Perl XS and Inline::CGetting started with Perl XS and Inline::C
Getting started with Perl XS and Inline::Cdaoswald
 
Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)
Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)
Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)ngotogenome
 
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory CourseRuby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Coursepeter_marklund
 
ZFConf 2012: Dependency Management в PHP и Zend Framework 2 (Кирилл Чебунин)
ZFConf 2012: Dependency Management в PHP и Zend Framework 2 (Кирилл Чебунин)ZFConf 2012: Dependency Management в PHP и Zend Framework 2 (Кирилл Чебунин)
ZFConf 2012: Dependency Management в PHP и Zend Framework 2 (Кирилл Чебунин)ZFConf Conference
 
Practical Testing of Ruby Core
Practical Testing of Ruby CorePractical Testing of Ruby Core
Practical Testing of Ruby CoreHiroshi SHIBATA
 
Middleware as Code with mruby
Middleware as Code with mrubyMiddleware as Code with mruby
Middleware as Code with mrubyHiroshi SHIBATA
 
Security Goodness with Ruby on Rails
Security Goodness with Ruby on RailsSecurity Goodness with Ruby on Rails
Security Goodness with Ruby on RailsSource Conference
 
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015Fernando Hamasaki de Amorim
 

What's hot (18)

javascript teach
javascript teachjavascript teach
javascript teach
 
Open Source Compiler Construction for the JVM
Open Source Compiler Construction for the JVMOpen Source Compiler Construction for the JVM
Open Source Compiler Construction for the JVM
 
JRuby @ Boulder Ruby
JRuby @ Boulder RubyJRuby @ Boulder Ruby
JRuby @ Boulder Ruby
 
MySQL Proxy tutorial
MySQL Proxy tutorialMySQL Proxy tutorial
MySQL Proxy tutorial
 
Going to Mars with Groovy Domain-Specific Languages
Going to Mars with Groovy Domain-Specific LanguagesGoing to Mars with Groovy Domain-Specific Languages
Going to Mars with Groovy Domain-Specific Languages
 
Debugging on rails
Debugging on railsDebugging on rails
Debugging on rails
 
Connecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyConnecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRuby
 
Getting started with Perl XS and Inline::C
Getting started with Perl XS and Inline::CGetting started with Perl XS and Inline::C
Getting started with Perl XS and Inline::C
 
Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)
Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)
Running Ruby on Solaris (RubyKaigi 2015, 12/Dec/2015)
 
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory CourseRuby on Rails 101 - Presentation Slides for a Five Day Introductory Course
Ruby on Rails 101 - Presentation Slides for a Five Day Introductory Course
 
Beyond Phoenix
Beyond PhoenixBeyond Phoenix
Beyond Phoenix
 
ZFConf 2012: Dependency Management в PHP и Zend Framework 2 (Кирилл Чебунин)
ZFConf 2012: Dependency Management в PHP и Zend Framework 2 (Кирилл Чебунин)ZFConf 2012: Dependency Management в PHP и Zend Framework 2 (Кирилл Чебунин)
ZFConf 2012: Dependency Management в PHP и Zend Framework 2 (Кирилл Чебунин)
 
Practical Testing of Ruby Core
Practical Testing of Ruby CorePractical Testing of Ruby Core
Practical Testing of Ruby Core
 
DSLs in JavaScript
DSLs in JavaScriptDSLs in JavaScript
DSLs in JavaScript
 
Middleware as Code with mruby
Middleware as Code with mrubyMiddleware as Code with mruby
Middleware as Code with mruby
 
Security Goodness with Ruby on Rails
Security Goodness with Ruby on RailsSecurity Goodness with Ruby on Rails
Security Goodness with Ruby on Rails
 
RubyGems 3 & 4
RubyGems 3 & 4RubyGems 3 & 4
RubyGems 3 & 4
 
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
The worst Ruby codes I’ve seen in my life - RubyKaigi 2015
 

Viewers also liked

Viewers also liked (6)

JRuby in the enterprise
JRuby in the enterpriseJRuby in the enterprise
JRuby in the enterprise
 
Groovier Selenium (Djug)
Groovier Selenium (Djug)Groovier Selenium (Djug)
Groovier Selenium (Djug)
 
Introduction to asyncio
Introduction to asyncioIntroduction to asyncio
Introduction to asyncio
 
Wind Show
Wind ShowWind Show
Wind Show
 
A Curious Course on Coroutines and Concurrency
A Curious Course on Coroutines and ConcurrencyA Curious Course on Coroutines and Concurrency
A Curious Course on Coroutines and Concurrency
 
Over 9000: JRuby in 2015
Over 9000: JRuby in 2015Over 9000: JRuby in 2015
Over 9000: JRuby in 2015
 

Similar to Quick Intro To JRuby

Hacking with ruby2ruby
Hacking with ruby2rubyHacking with ruby2ruby
Hacking with ruby2rubyMarc Chung
 
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011Nick Sieger
 
JRuby - Enterprise 2.0
JRuby - Enterprise 2.0JRuby - Enterprise 2.0
JRuby - Enterprise 2.0Jan Sifra
 
Monkeybars in the Manor
Monkeybars in the ManorMonkeybars in the Manor
Monkeybars in the Manormartinbtt
 
Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007Guillaume Laforge
 
Cより速いRubyプログラム
Cより速いRubyプログラムCより速いRubyプログラム
Cより速いRubyプログラムkwatch
 
All I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web FrameworkAll I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web FrameworkBen Scofield
 
Adventurous Merb
Adventurous MerbAdventurous Merb
Adventurous MerbMatt Todd
 
Migrating To Ruby1.9
Migrating To Ruby1.9Migrating To Ruby1.9
Migrating To Ruby1.9tomaspavelka
 
Get Soaked - An In Depth Look At PHP Streams
Get Soaked - An In Depth Look At PHP StreamsGet Soaked - An In Depth Look At PHP Streams
Get Soaked - An In Depth Look At PHP StreamsDavey Shafik
 
The Enterprise Strikes Back
The Enterprise Strikes BackThe Enterprise Strikes Back
The Enterprise Strikes BackBurke Libbey
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing UpDavid Padbury
 
JRuby e DSL
JRuby e DSLJRuby e DSL
JRuby e DSLjodosha
 

Similar to Quick Intro To JRuby (20)

Intro to J Ruby
Intro to J RubyIntro to J Ruby
Intro to J Ruby
 
Hacking with ruby2ruby
Hacking with ruby2rubyHacking with ruby2ruby
Hacking with ruby2ruby
 
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
 
JRuby - Enterprise 2.0
JRuby - Enterprise 2.0JRuby - Enterprise 2.0
JRuby - Enterprise 2.0
 
Jet presentation
Jet presentationJet presentation
Jet presentation
 
JS everywhere 2011
JS everywhere 2011JS everywhere 2011
JS everywhere 2011
 
Monkeybars in the Manor
Monkeybars in the ManorMonkeybars in the Manor
Monkeybars in the Manor
 
Ruby on Rails Presentation
Ruby on Rails PresentationRuby on Rails Presentation
Ruby on Rails Presentation
 
Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007
 
Node.js vs Play Framework
Node.js vs Play FrameworkNode.js vs Play Framework
Node.js vs Play Framework
 
Cより速いRubyプログラム
Cより速いRubyプログラムCより速いRubyプログラム
Cより速いRubyプログラム
 
All I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web FrameworkAll I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web Framework
 
Having Fun with Play
Having Fun with PlayHaving Fun with Play
Having Fun with Play
 
Adventurous Merb
Adventurous MerbAdventurous Merb
Adventurous Merb
 
Migrating To Ruby1.9
Migrating To Ruby1.9Migrating To Ruby1.9
Migrating To Ruby1.9
 
Get Soaked - An In Depth Look At PHP Streams
Get Soaked - An In Depth Look At PHP StreamsGet Soaked - An In Depth Look At PHP Streams
Get Soaked - An In Depth Look At PHP Streams
 
The Enterprise Strikes Back
The Enterprise Strikes BackThe Enterprise Strikes Back
The Enterprise Strikes Back
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
 
Ugo Cei Presentation
Ugo Cei PresentationUgo Cei Presentation
Ugo Cei Presentation
 
JRuby e DSL
JRuby e DSLJRuby e DSL
JRuby e DSL
 

Recently uploaded

Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 

Recently uploaded (20)

Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 

Quick Intro To JRuby

  • 1. A (Quick) Introduction to JRuby Frederic Jean fred@fredjean.net
  • 3. Develop, Deploy, Build, Test, Monitor Configuration DSLs Glue, Wiring External System Integration Dynamic Layer Application Code Stable Layer Java, Statically typed Legacy libraries
  • 5. Ruby is... • Dynamically Typed • Strongly Typed • Everything is An Object
  • 7. Ruby Literals # Strings quot;This is a Stringquot; 'This is also a String' %{So is this} # Numbers 1, 0xa2, 0644, 3.14, 2.2e10 # Arrays and Hashes ['John', 'Henry', 'Mark'] { :hello => quot;bonjourquot;, :bye => quot;au revoirquot;} # Regular Expressions quot;Mary had a little lambquot; =~ /little/ quot;The London Bridgequot; =~ %r{london}i # Ranges (1..100) # inclusive (1...100) # exclusive
  • 8. Symbols :symbol • String-like construct • Only one copy in memory
  • 9. Blocks ['John', 'Henry', 'Mark'].each { |name| puts quot;#{name}quot;} File.open(quot;/tmp/password.txtquot;) do |input| text = input.read end
  • 10. Ruby Classes class Person attr_accessor :name, :title end p = Person.new
  • 11. Open Classes class Fixnum def odd? self % 2 == 1 end def even? self % 2 == 0 end end http://localhost:4567/code/fixnum.rb
  • 12. Mixins module Parity def even? self % 2 == 0 end def odd? self % 2 == 1 end end http://localhost:4567/code/parity.rb
  • 13. Mixins class Person include Enumerable attr_accessor :first_name, :last_name def initialize(first_name, last_name) @first_name = first_name @last_name = last_name end def <=>(other) if self.last_name == other.last_name self.first_name <=> other.first_name else self.last_name <=> other.last_name end end def to_s quot;#{first_name} #{last_name}quot; end end http://localhost:4567/code/sorting.rb
  • 14. JRuby
  • 15. JRuby • Ruby Implementation on the JVM • Compatible with Ruby 1.8.6 • Native Multithreading • Full Access to Java Libraries
  • 16. JRuby Hello World java.lang.System.out.println quot;Hello JRuby!quot; http://localhost:4567/code/jruby_hello.rb
  • 18. JI: Ruby-like Methods Java Ruby Locale.US Locale::US System.currentTimeMillis() System.current_time_millis locale.getLanguage() locale.language date.getTime() date.time date.setTime(10) date.time = 10 file.isDirectory() file.directory?
  • 19. JI: Java Extensions h = java.util.HashMap.new h[quot;keyquot;] = quot;valuequot; h[quot;keyquot;] # => quot;valuequot; h.get(quot;keyquot;) # => quot;valuequot; h.each {|k,v| puts k + ' => ' + v} # key => value h.to_a # => [[quot;keyquot;, quot;valuequot;]] http://localhost:4567/code/ji/extensions.rb
  • 20. JI: Java Extensions module java::util::Map include Enumerable def each(&block) entrySet.each { |pair| block.call([pair.key, pair.value]) } end def [](key) get(key) end def []=(key,val) put(key,val) val end end
  • 21. JI: Open Java Classes class Java::JavaLang::Integer def even? int_value % 2 == 0 end def odd? int_value % 2 == 1 end end http://localhost:4567/code/ji/integer.rb
  • 22. JI: Interface Conversion package java.util.concurrent; public class Executors { // ... public static Callable callable(Runnable r) { // ... } }
  • 23. JI: Interface Conversion class SimpleRubyObject def run puts quot;hiquot; end end callable = Executors.callable(SimpleRubyObject.new) callable.call http://localhost:4567/code/ji/if_conversion.rb
  • 24. JI: Closure Conversion callable = Executors.callable { puts quot;hiquot; } callable.call http://localhost:4567/code/ji/closure_conversion.rb
  • 25. Develop, Deploy, Build, Test, Monitor Configuration DSLs Glue, Wiring External System Integration Dynamic Layer Application Code Stable Layer Java, Statically typed Legacy libraries
  • 26. Usage • Runtime • Wrapper around Java Code • Calling From Java • Utilities
  • 27. JRuby as a Runtime
  • 28. JRuby on Rails • Wrap Rails app into a war file • Direct support in Glassfish v3 Prelude • http://kenai.com • http://mediacast.sun.com
  • 29. webmate require 'rubygems' require 'sinatra' get '/*' do file, line = request.path_info.split(/:/) local_file = File.join(Dir.pwd, file) if (File.exists?(local_file)) redirect quot;txmt://open/?url=file://#{local_file}&line=#{line}quot; else not_found end end http://localhost:4567/code/webmate.rb
  • 31. Swing Applications frame = JFrame.new(quot;Hello Swingquot;) • Reduce verbosity • Simplify event handlers http://localhost:4567/code/swing.rb
  • 32. RSpec Testing Java describe quot;An emptyquot;, HashMap do before :each do @hash_map = HashMap.new end it quot;should be able to add an entry to itquot; do @hash_map.put quot;fooquot;, quot;barquot; @hash_map.get(quot;fooquot;).should == quot;barquot; end end JTestr Provides Ant and Maven integration http://jtestr.codehaus.org http://localhost:4567/code/hashmap_spec.rb
  • 34. Develop, Deploy, Build, Test, Monitor Configuration DSLs Glue, Wiring External System Integration Dynamic Layer Application Code Stable Layer Java, Statically typed Legacy libraries
  • 35. Build Utilities • Rake • Raven • Buildr
  • 37. Resources • http://jruby.org • http://wiki.jruby.org • http://jruby.kenai.com • http://jtester.codehaus.org • http://raven.rubyforge.org • http://buildr.apache.org