Download - Intro to J Ruby
![Page 1: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/1.jpg)
Introduction to JRubyFrederic Jean
1
![Page 2: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/2.jpg)
First Encounter
2
![Page 3: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/3.jpg)
Hello JRuby
3
![Page 4: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/4.jpg)
http://olabini.com/blog/2008/01/language-explorations/
Stable LayerJava, Statically typed
Legacy libraries
Dynamic LayerApplication Code
DSLsConfigurationGlue, Wiring
4
![Page 5: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/5.jpg)
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
![Page 6: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/6.jpg)
Ruby
6
![Page 7: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/7.jpg)
Ruby is...
• Dynamically Typed
• Strongly Typed
• Everything is An Object
7
![Page 8: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/8.jpg)
Dynamic Typing
• Variables Are Not Typed
• Type Evaluated at Run Time
• Duck Typing
8
![Page 9: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/9.jpg)
(Almost) Everything is an Object
9
![Page 10: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/10.jpg)
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
![Page 11: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/11.jpg)
Symbols
• String-like construct
• Only one copy in memory
:symbol
11
![Page 12: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/12.jpg)
Ruby Type
"hello".class # => String1.class # => Fixnum10.3.class # => Float[].class # => Array{}.class # => HashKernel.class # => ModuleObject.class # => Class:symbol.class # => Symbol
12
![Page 13: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/13.jpg)
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
![Page 14: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/14.jpg)
Blocks
['John', 'Henry', 'Mark'].each { |name| puts "#{name}"}
File.open("/tmp/password.txt") do |input| text = input.readend
14
![Page 15: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/15.jpg)
Ruby Classes
class Person attr_accessor :name, :titleend
p = Person.new
15
![Page 16: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/16.jpg)
Open Classesclass Fixnum def odd? self % 2 == 1 end def even? self % 2 == 0 endend
http://localhost:4567/code/fixnum.rb16
![Page 17: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/17.jpg)
Namespacing
module Net module FredJean class MyClass end endend
Net::FredJean::MyClass
17
![Page 18: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/18.jpg)
Mixinsmodule Parity def even? self % 2 == 0 end def odd? self % 2 == 1 endend
http://localhost:4567/code/parity.rb18
![Page 19: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/19.jpg)
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
![Page 20: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/20.jpg)
Mixin Reflection
puts String.include? Enumerableputs String.instance_of? Enumerableputs String.kind_of? Enumerable
http://localhost:4567/code/include.rb20
![Page 21: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/21.jpg)
Executable Class Definition
class Message if RUBY_PLATFORM =~ /java/i def hello puts "Hello From JRuby" end endend
http://localhost:4567/code/env.rb21
![Page 22: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/22.jpg)
JRuby
22
![Page 23: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/23.jpg)
JRuby
• Ruby Implementation on the JVM
• Compatible with Ruby 1.8.6
• Native Multithreading
• Full Access to Java Libraries
23
![Page 24: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/24.jpg)
JRuby Hello World
java.lang.System.out.println "Hello JRuby!"
http://localhost:4567/code/jruby_hello.rb24
![Page 25: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/25.jpg)
Java Integration
require 'java'
25
![Page 26: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/26.jpg)
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
![Page 27: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/27.jpg)
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
![Page 28: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/28.jpg)
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
![Page 29: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/29.jpg)
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
![Page 30: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/30.jpg)
JI: Interface Conversion
package java.util.concurrent;
public class Executors { // ... public static Callable callable(Runnable r) { // ... }}
30
![Page 31: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/31.jpg)
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
![Page 32: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/32.jpg)
JI: Closure Conversion
callable = Executors.callable { puts "hi" }callable.call
http://localhost:4567/code/ji/closure_conversion.rb32
![Page 33: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/33.jpg)
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
![Page 34: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/34.jpg)
Usage
• Runtime
• Wrapper around Java Code
• Calling From Java
• Utilities
34
![Page 35: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/35.jpg)
JRuby as a Runtime
35
![Page 36: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/36.jpg)
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
![Page 37: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/37.jpg)
Warbler
jruby -S gem install warblerjruby -S warble config# => config/warble.rbjruby -S warble# => myapp.war
37
![Page 38: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/38.jpg)
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
![Page 39: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/39.jpg)
Wrapping Java Code
39
![Page 40: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/40.jpg)
Swing Applications
• Reduce verbosity
• Simplify event handlers
frame = JFrame.new("Hello Swing")
http://localhost:4567/code/swing.rb40
![Page 41: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/41.jpg)
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
![Page 42: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/42.jpg)
Java Calling JRuby
42
![Page 43: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/43.jpg)
JSR-223
• Ships with Java 6
• Supports JRuby 1.1
https://scripting.dev.java.net/
43
![Page 44: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/44.jpg)
JRuby Runtime
• Support the current release
• Specific to JRuby
• Subject to change
44
![Page 45: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/45.jpg)
Spring Scripting
<lang:jruby id="lime" script-interfaces="springruby.Lime" script-source="classpath:lime.rb"/>
45
![Page 46: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/46.jpg)
Utilities
46
![Page 47: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/47.jpg)
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
![Page 48: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/48.jpg)
Build Utilities
• Rake
• Raven
• Buildr
48
![Page 49: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/49.jpg)
Conclusion
49
![Page 50: Intro to J Ruby](https://reader030.vdocuments.net/reader030/viewer/2022020219/55d507a9bb61eb46338b4727/html5/thumbnails/50.jpg)
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