intro to j ruby
TRANSCRIPT
Introduction to JRubyFrederic Jean
1
First Encounter
2
Hello JRuby
3
http://olabini.com/blog/2008/01/language-explorations/
Stable LayerJava, Statically typed
Legacy libraries
Dynamic LayerApplication Code
DSLsConfigurationGlue, Wiring
4
Stable LayerJava, Statically typed
Legacy libraries
Dynamic LayerApplication Code
DSLsConfigurationGlue, Wiring
Exte
rnal S
yste
m In
teg
rati
on
Develo
p, D
ep
loy, B
uild
, Test, M
on
itor
5
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"This is a String"'This is also a String'%{So is this}
# Numbers1, 0xa2, 0644, 3.14, 2.2e10
# Arrays and Hashes['John', 'Henry', 'Mark']{ :hello => "bonjour", :bye => "au revoir"}
# Regular Expressions"Mary had a little lamb" =~ /little/"The London Bridge" =~ %r{london}i
# Ranges(1..100) # inclusive(1...100) # exclusive
10
Symbols
• String-like construct
• Only one copy in memory
:symbol
11
Ruby Type
"hello".class # => String1.class # => Fixnum10.3.class # => Float[].class # => Array{}.class # => HashKernel.class # => ModuleObject.class # => Class:symbol.class # => Symbol
12
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.rb13
Blocks
['John', 'Henry', 'Mark'].each { |name| puts "#{name}"}
File.open("/tmp/password.txt") do |input| text = input.readend
14
Ruby Classes
class Person attr_accessor :name, :titleend
p = Person.new
15
Open Classesclass Fixnum def odd? self % 2 == 1 end def even? self % 2 == 0 endend
http://localhost:4567/code/fixnum.rb16
Namespacing
module Net module FredJean class MyClass end endend
Net::FredJean::MyClass
17
Mixinsmodule Parity def even? self % 2 == 0 end def odd? self % 2 == 1 endend
http://localhost:4567/code/parity.rb18
Mixinsclass 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 "#{first_name} #{last_name}" endend
http://localhost:4567/code/sorting.rb19
Mixin Reflection
puts String.include? Enumerableputs String.instance_of? Enumerableputs String.kind_of? Enumerable
http://localhost:4567/code/include.rb20
Executable Class Definition
class Message if RUBY_PLATFORM =~ /java/i def hello puts "Hello From JRuby" end endend
http://localhost:4567/code/env.rb21
JRuby
22
JRuby
• Ruby Implementation on the JVM
• Compatible with Ruby 1.8.6
• Native Multithreading
• Full Access to Java Libraries
23
JRuby Hello World
java.lang.System.out.println "Hello JRuby!"
http://localhost:4567/code/jruby_hello.rb24
Java Integration
require 'java'
25
JI: Ruby-like Methods
Locale.USSystem.currentTimeMillis()locale.getLanguage()date.getTime()date.setTime(10)file.isDirectory()
Locale::USSystem.current_time_millislocale.languagedate.timedate.time = 10file.directory?
Java Ruby
26
JI: Java Extensions
h = java.util.HashMap.newh["key"] = "value"h["key"]# => "value"h.get("key")# => "value"h.each {|k,v| puts k + ' => ' + v}# key => valueh.to_a# => [["key", "value"]]
http://localhost:4567/code/ji/extensions.rb27
JI: Java Extensionsmodule 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 endend
28
class Java::JavaLang::Integer def even? int_value % 2 == 0 end def odd? int_value % 2 == 1 endend
JI: Open Java Classes
http://localhost:4567/code/ji/integer.rb29
JI: Interface Conversion
package java.util.concurrent;
public class Executors { // ... public static Callable callable(Runnable r) { // ... }}
30
JI: Interface Conversion
class SimpleRubyObject def run puts "hi" endend
callable = Executors.callable(SimpleRubyObject.new)callable.call
http://localhost:4567/code/ji/if_conversion.rb31
JI: Closure Conversion
callable = Executors.callable { puts "hi" }callable.call
http://localhost:4567/code/ji/closure_conversion.rb32
Stable LayerJava, Statically typed
Legacy libraries
Dynamic LayerApplication Code
DSLsConfigurationGlue, Wiring
Exte
rnal S
yste
m In
teg
rati
on
Develo
p, D
ep
loy, B
uild
, Test, M
on
itor
33
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
• http://mediacast.sun.com
36
Warbler
jruby -S gem install warblerjruby -S warble config# => config/warble.rbjruby -S warble# => myapp.war
37
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 "txmt://open/?url=file://#{local_file}&line=#{line}" else not_found endend
http://localhost:4567/code/webmate.rb38
Wrapping Java Code
39
Swing Applications
• Reduce verbosity
• Simplify event handlers
frame = JFrame.new("Hello Swing")
http://localhost:4567/code/swing.rb40
RSpec Testing Javadescribe "An empty", HashMap do before :each do @hash_map = HashMap.new end
it "should be able to add an entry to it" do @hash_map.put "foo", "bar" @hash_map.get("foo").should == "bar" endend
JTestr Provides Ant and Maven integrationhttp://jtestr.codehaus.org
http://localhost:4567/code/hashmap_spec.rb41
Java Calling JRuby
42
JSR-223
• Ships with Java 6
• Supports JRuby 1.1
https://scripting.dev.java.net/
43
JRuby Runtime
• Support the current release
• Specific to JRuby
• Subject to change
44
Spring Scripting
<lang:jruby id="lime" script-interfaces="springruby.Lime" script-source="classpath:lime.rb"/>
45
Utilities
46
Stable LayerJava, Statically typed
Legacy libraries
Dynamic LayerApplication Code
DSLsConfigurationGlue, Wiring
Exte
rnal S
yste
m In
teg
rati
on
Develo
p, D
ep
loy, B
uild
, Test, M
on
itor
47
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.rubyforge.org
• http://buildr.apache.org
50