Introduction to
     JRuby
       Frederic Jean
    fred@fredjean.net




                        1
First Encounter



                  2
Hello JRuby



              3
Configuration
                 DSLs
                                     Glue, Wiring




                         Dynamic ...
Develop, Deploy, Build, Test, Monitor
                                                  Configuration
                     ...
Ruby



       6
Ruby is...

• Dynamically Typed
• Strongly Typed
• Everything is An Object


                            7
Dynamic Typing

• Variables Are Not Typed
• Type Evaluated at Run Time
• Duck Typing


                               8
(Almost) Everything is
     an Object


                         9
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

                            11
Ruby Type
quot;helloquot;.class   #   =>   String
1.class         #   =>   Fixnum
10.3.class      #   =>   Float
[].class ...
kind_of? vs respond_to?
   puts 10.instance_of? Fixnum

   puts 10.respond_to? :odd?
   puts 10.respond_to? :between?




...
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




                                15
Open Classes
     class Fixnum
       def odd?
         self % 2 == 1
       end

       def even?
         self % 2 == 0
...
Namespacing
module Net
  module FredJean
    class MyClass
    end
  end
end

Net::FredJean::MyClass




                 ...
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, last_name)...
Mixin Reflection

puts String.include? Enumerable
puts String.instance_of? Enumerable
puts String.kind_of? Enumerable




 ...
Executable Class Definition
   class Message
     if RUBY_PLATFORM =~ /java/i
       def hello
         puts quot;Hello Fro...
JRuby



        22
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'




                   25
JI: Ruby-like Methods
           Java                        Ruby
Locale.US                    Locale::US
System.currentTi...
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 c...
JI: Interface Conversion
class SimpleRubyObject
  def run
    puts quot;hiquot;
  end
end

callable = Executors.callable(S...
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

                             34
JRuby as a Runtime



                     35
JRuby on Rails

• Ruby on Rails 2.1 and above is supported
• Direct support in Glassfish v3 Prelude
• http://kenai.com
• ht...
Warbler

jruby -S gem install warbler
jruby -S warble config
# => config/warble.rb
jruby -S warble
# => myapp.war




      ...
webmate
require 'rubygems'
require 'sinatra'

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



                     39
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 ...
Java Calling JRuby



                     42
JSR-223

• Ships with Java 6
• Supports JRuby 1.1
        https://scripting.dev.java.net/



                             ...
JRuby Runtime

• Support the current release
• Specific to JRuby
• Subject to change


                                44
Spring Scripting

<lang:jruby id=quot;limequot; script-interfaces=quot;springruby.Limequot;
        script-source=quot;cla...
Utilities



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

• Rake
• Raven
• Buildr


                             48
Conclusion



             49
Resources
• http://jruby.org
• http://wiki.jruby.org
• http://jruby.kenai.com
• http://jtester.codehaus.org
• http://raven...
Upcoming SlideShare
Loading in …5
×

Intro to J Ruby

943
-1

Published on

Slides for my Introduction to JRuby talk.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
943
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
19
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Intro to J Ruby

  1. 1. Introduction to JRuby Frederic Jean fred@fredjean.net 1
  2. 2. First Encounter 2
  3. 3. Hello JRuby 3
  4. 4. Configuration DSLs Glue, Wiring Dynamic Layer Application Code Stable Layer Java, Statically typed Legacy libraries http://olabini.com/blog/2008/01/language-explorations/ 4
  5. 5. Develop, Deploy, Build, Test, Monitor Configuration DSLs Glue, Wiring External System Integration Dynamic Layer Application Code Stable Layer Java, Statically typed Legacy libraries 5
  6. 6. Ruby 6
  7. 7. Ruby is... • Dynamically Typed • Strongly Typed • Everything is An Object 7
  8. 8. Dynamic Typing • Variables Are Not Typed • Type Evaluated at Run Time • Duck Typing 8
  9. 9. (Almost) Everything is an Object 9
  10. 10. 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 10
  11. 11. Symbols :symbol • String-like construct • Only one copy in memory 11
  12. 12. Ruby Type quot;helloquot;.class # => String 1.class # => Fixnum 10.3.class # => Float [].class # => Array {}.class # => Hash Kernel.class # => Module Object.class # => Class :symbol.class # => Symbol 12
  13. 13. kind_of? vs respond_to? puts 10.instance_of? Fixnum puts 10.respond_to? :odd? puts 10.respond_to? :between? http://localhost:4567/code/instance_of.rb 13
  14. 14. Blocks ['John', 'Henry', 'Mark'].each { |name| puts quot;#{name}quot;} File.open(quot;/tmp/password.txtquot;) do |input| text = input.read end 14
  15. 15. Ruby Classes class Person attr_accessor :name, :title end p = Person.new 15
  16. 16. Open Classes class Fixnum def odd? self % 2 == 1 end def even? self % 2 == 0 end end http://localhost:4567/code/fixnum.rb 16
  17. 17. Namespacing module Net module FredJean class MyClass end end end Net::FredJean::MyClass 17
  18. 18. Mixins module Parity def even? self % 2 == 0 end def odd? self % 2 == 1 end end http://localhost:4567/code/parity.rb 18
  19. 19. 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) self.last_name <=> other.last_name && self.first_name <=> other.last_name end def to_s quot;#{first_name} #{last_name}quot; end end http://localhost:4567/code/sorting.rb 19
  20. 20. Mixin Reflection puts String.include? Enumerable puts String.instance_of? Enumerable puts String.kind_of? Enumerable http://localhost:4567/code/include.rb 20
  21. 21. Executable Class Definition class Message if RUBY_PLATFORM =~ /java/i def hello puts quot;Hello From JRubyquot; end end end http://localhost:4567/code/env.rb 21
  22. 22. JRuby 22
  23. 23. JRuby • Ruby Implementation on the JVM • Compatible with Ruby 1.8.6 • Native Multithreading • Full Access to Java Libraries 23
  24. 24. JRuby Hello World java.lang.System.out.println quot;Hello JRuby!quot; http://localhost:4567/code/jruby_hello.rb 24
  25. 25. Java Integration require 'java' 25
  26. 26. 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? 26
  27. 27. 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 27
  28. 28. 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 28
  29. 29. 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 29
  30. 30. JI: Interface Conversion package java.util.concurrent; public class Executors { // ... public static Callable callable(Runnable r) { // ... } } 30
  31. 31. 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 31
  32. 32. JI: Closure Conversion callable = Executors.callable { puts quot;hiquot; } callable.call http://localhost:4567/code/ji/closure_conversion.rb 32
  33. 33. Develop, Deploy, Build, Test, Monitor Configuration DSLs Glue, Wiring External System Integration Dynamic Layer Application Code Stable Layer Java, Statically typed Legacy libraries 33
  34. 34. Usage • Runtime • Wrapper around Java Code • Calling From Java • Utilities 34
  35. 35. JRuby as a Runtime 35
  36. 36. JRuby on Rails • Ruby on Rails 2.1 and above is supported • Direct support in Glassfish v3 Prelude • http://kenai.com • http://mediacast.sun.com 36
  37. 37. Warbler jruby -S gem install warbler jruby -S warble config # => config/warble.rb jruby -S warble # => myapp.war 37
  38. 38. 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 38
  39. 39. Wrapping Java Code 39
  40. 40. Swing Applications frame = JFrame.new(quot;Hello Swingquot;) • Reduce verbosity • Simplify event handlers http://localhost:4567/code/swing.rb 40
  41. 41. 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 41
  42. 42. Java Calling JRuby 42
  43. 43. JSR-223 • Ships with Java 6 • Supports JRuby 1.1 https://scripting.dev.java.net/ 43
  44. 44. JRuby Runtime • Support the current release • Specific to JRuby • Subject to change 44
  45. 45. Spring Scripting <lang:jruby id=quot;limequot; script-interfaces=quot;springruby.Limequot; script-source=quot;classpath:lime.rbquot;/> 45
  46. 46. Utilities 46
  47. 47. Develop, Deploy, Build, Test, Monitor Configuration DSLs Glue, Wiring External System Integration Dynamic Layer Application Code Stable Layer Java, Statically typed Legacy libraries 47
  48. 48. Build Utilities • Rake • Raven • Buildr 48
  49. 49. Conclusion 49
  50. 50. Resources • http://jruby.org • http://wiki.jruby.org • http://jruby.kenai.com • http://jtester.codehaus.org • http://raven.rubyforge.org • http://buildr.apache.org 50
  1. A particular slide catching your eye?

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

×