intro to j ruby

50
Introduction to JRuby Frederic Jean [email protected] 1

Upload: frederic-jean

Post on 20-Aug-2015

906 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Intro to J Ruby

Introduction to JRubyFrederic Jean

[email protected]

1

Page 2: Intro to J Ruby

First Encounter

2

Page 3: Intro to J Ruby

Hello JRuby

3

Page 4: Intro to J Ruby

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

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

Ruby

6

Page 7: Intro to J Ruby

Ruby is...

• Dynamically Typed

• Strongly Typed

• Everything is An Object

7

Page 8: Intro to J Ruby

Dynamic Typing

• Variables Are Not Typed

• Type Evaluated at Run Time

• Duck Typing

8

Page 9: Intro to J Ruby

(Almost) Everything is an Object

9

Page 10: Intro to J Ruby

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

Symbols

• String-like construct

• Only one copy in memory

:symbol

11

Page 12: Intro to J Ruby

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

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

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

Ruby Classes

class Person attr_accessor :name, :titleend

p = Person.new

15

Page 16: Intro to J Ruby

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

Namespacing

module Net module FredJean class MyClass end endend

Net::FredJean::MyClass

17

Page 18: Intro to J Ruby

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

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

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

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

JRuby

22

Page 23: Intro to J Ruby

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

JRuby Hello World

java.lang.System.out.println "Hello JRuby!"

http://localhost:4567/code/jruby_hello.rb24

Page 25: Intro to J Ruby

Java Integration

require 'java'

25

Page 26: Intro to J Ruby

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

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

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

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

JI: Interface Conversion

package java.util.concurrent;

public class Executors { // ... public static Callable callable(Runnable r) { // ... }}

30

Page 31: Intro to J Ruby

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

JI: Closure Conversion

callable = Executors.callable { puts "hi" }callable.call

http://localhost:4567/code/ji/closure_conversion.rb32

Page 33: Intro to J Ruby

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

Usage

• Runtime

• Wrapper around Java Code

• Calling From Java

• Utilities

34

Page 35: Intro to J Ruby

JRuby as a Runtime

35

Page 36: Intro to J Ruby

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

Warbler

jruby -S gem install warblerjruby -S warble config# => config/warble.rbjruby -S warble# => myapp.war

37

Page 38: Intro to J Ruby

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

Wrapping Java Code

39

Page 40: Intro to J Ruby

Swing Applications

• Reduce verbosity

• Simplify event handlers

frame = JFrame.new("Hello Swing")

http://localhost:4567/code/swing.rb40

Page 41: Intro to J Ruby

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

Java Calling JRuby

42

Page 43: Intro to J Ruby

JSR-223

• Ships with Java 6

• Supports JRuby 1.1

https://scripting.dev.java.net/

43

Page 44: Intro to J Ruby

JRuby Runtime

• Support the current release

• Specific to JRuby

• Subject to change

44

Page 45: Intro to J Ruby

Spring Scripting

<lang:jruby id="lime" script-interfaces="springruby.Lime" script-source="classpath:lime.rb"/>

45

Page 46: Intro to J Ruby

Utilities

46

Page 47: Intro to J Ruby

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

Build Utilities

• Rake

• Raven

• Buildr

48

Page 49: Intro to J Ruby

Conclusion

49

Page 50: Intro to J Ruby

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