A (Quick) Introduction
      to JRuby
          Frederic Jean
       fred@fredjean.net
Hello JRuby
Develop, Deploy, Build, Test, Monitor
                                                  Configuration
                     ...
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...
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|...
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...
Mixins
     module Parity
       def even?
         self % 2 == 0
       end

       def odd?
         self % 2 == 1
     ...
Mixins
   class Person
     include Enumerable

     attr_accessor :first_name, :last_name

     def initialize(first_name...
JRuby
JRuby

• Ruby Implementation on the JVM
• Compatible with Ruby 1.8.6
• Native Multithreading
• Full Access to Java Librari...
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.currentT...
JI: Java Extensions

      h = java.util.HashMap.new
      h[quot;keyquot;] = quot;valuequot;
      h[quot;keyquot;]
     ...
JI: Java Extensions
module java::util::Map
  include Enumerable

  def each(&block)
    entrySet.each { |pair| block.call(...
JI: Open Java Classes
class Java::JavaLang::Integer
  def even?
    int_value % 2 == 0
  end

  def odd?
    int_value % 2...
JI: Interface Conversion

 package java.util.concurrent;

 public class Executors {
    // ...
    public static Callable ...
JI: Interface Conversion

class SimpleRubyObject
  def run
    puts quot;hiquot;
  end
end

callable = Executors.callable(...
JI: Closure Conversion


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




  http://localhost:4567/cod...
Develop, Deploy, Build, Test, Monitor
                                                  Configuration
                     ...
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://mediac...
webmate

require 'rubygems'
require 'sinatra'

get '/*' do
  file, line = request.path_info.split(/:/)
  local_file = File...
Wrapping Java Code
Swing Applications

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


   • Reduce verbosity
   • Simplify event handlers
       ...
RSpec Testing Java
 describe quot;An emptyquot;, HashMap do
   before :each do
     @hash_map = HashMap.new
   end

   it ...
Utilities
Develop, Deploy, Build, Test, Monitor
                                                  Configuration
                     ...
Build Utilities

• Rake
• Raven
• Buildr
Conclusion
Resources
• http://jruby.org
• http://wiki.jruby.org
• http://jruby.kenai.com
• http://jtester.codehaus.org
• http://raven...
Upcoming SlideShare
Loading in...5
×

Quick Intro To JRuby

3,408

Published on

A shorter version of my Introduction to JRuby talk that was given at the Colorado Springs Open Source User Group.

Published in: Technology, Education
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,408
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
48
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Quick Intro To JRuby

  1. 1. A (Quick) Introduction to JRuby Frederic Jean fred@fredjean.net
  2. 2. Hello JRuby
  3. 3. Develop, Deploy, Build, Test, Monitor Configuration DSLs Glue, Wiring External System Integration Dynamic Layer Application Code Stable Layer Java, Statically typed Legacy libraries
  4. 4. Ruby
  5. 5. Ruby is... • Dynamically Typed • Strongly Typed • Everything is An Object
  6. 6. (Almost) Everything is an Object
  7. 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. 8. Symbols :symbol • String-like construct • Only one copy in memory
  9. 9. Blocks ['John', 'Henry', 'Mark'].each { |name| puts quot;#{name}quot;} File.open(quot;/tmp/password.txtquot;) do |input| text = input.read end
  10. 10. Ruby Classes class Person attr_accessor :name, :title end p = Person.new
  11. 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. 12. Mixins module Parity def even? self % 2 == 0 end def odd? self % 2 == 1 end end http://localhost:4567/code/parity.rb
  13. 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. 14. JRuby
  15. 15. JRuby • Ruby Implementation on the JVM • Compatible with Ruby 1.8.6 • Native Multithreading • Full Access to Java Libraries
  16. 16. JRuby Hello World java.lang.System.out.println quot;Hello JRuby!quot; http://localhost:4567/code/jruby_hello.rb
  17. 17. Java Integration require 'java'
  18. 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. 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. 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. 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. 22. JI: Interface Conversion package java.util.concurrent; public class Executors { // ... public static Callable callable(Runnable r) { // ... } }
  23. 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. 24. JI: Closure Conversion callable = Executors.callable { puts quot;hiquot; } callable.call http://localhost:4567/code/ji/closure_conversion.rb
  25. 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. 26. Usage • Runtime • Wrapper around Java Code • Calling From Java • Utilities
  27. 27. JRuby as a Runtime
  28. 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. 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
  30. 30. Wrapping Java Code
  31. 31. Swing Applications frame = JFrame.new(quot;Hello Swingquot;) • Reduce verbosity • Simplify event handlers http://localhost:4567/code/swing.rb
  32. 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
  33. 33. Utilities
  34. 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. 35. Build Utilities • Rake • Raven • Buildr
  36. 36. Conclusion
  37. 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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×