quick intro to jruby

37
A (Quick) Introduction to JRuby Frederic Jean [email protected]

Upload: frederic-jean

Post on 15-May-2015

3.353 views

Category:

Technology


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Quick Intro To JRuby

A (Quick) Introduction to JRuby

Frederic [email protected]

Page 2: Quick Intro To JRuby

Hello JRuby

Page 3: Quick Intro To JRuby

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

Page 4: Quick Intro To JRuby

Ruby

Page 5: Quick Intro To JRuby

Ruby is...

• Dynamically Typed

• Strongly Typed

• Everything is An Object

Page 6: Quick Intro To JRuby

(Almost) Everything is an Object

Page 7: Quick Intro To JRuby

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

Page 8: Quick Intro To JRuby

Symbols

• String-like construct

• Only one copy in memory

:symbol

Page 9: Quick Intro To JRuby

Blocks

['John', 'Henry', 'Mark'].each { |name| puts "#{name}"}

File.open("/tmp/password.txt") do |input| text = input.readend

Page 10: Quick Intro To JRuby

Ruby Classes

class Person attr_accessor :name, :titleend

p = Person.new

Page 11: Quick Intro To JRuby

Open Classes

class Fixnum def odd? self % 2 == 1 end def even? self % 2 == 0 endend

http://localhost:4567/code/fixnum.rb

Page 12: Quick Intro To JRuby

Mixins

module Parity def even? self % 2 == 0 end def odd? self % 2 == 1 endend

http://localhost:4567/code/parity.rb

Page 13: Quick Intro To JRuby

Mixins

http://localhost:4567/code/sorting.rb

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 "#{first_name} #{last_name}" endend

Page 14: Quick Intro To JRuby

JRuby

Page 15: Quick Intro To JRuby

JRuby

• Ruby Implementation on the JVM

• Compatible with Ruby 1.8.6

• Native Multithreading

• Full Access to Java Libraries

Page 16: Quick Intro To JRuby

JRuby Hello World

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

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

Page 17: Quick Intro To JRuby

Java Integration

require 'java'

Page 18: Quick Intro To JRuby

Locale.USSystem.currentTimeMillis()locale.getLanguage()date.getTime()date.setTime(10)file.isDirectory()

Locale::USSystem.current_time_millislocale.languagedate.timedate.time = 10file.directory?

Java Ruby

JI: Ruby-like Methods

Page 19: Quick Intro To JRuby

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

Page 20: Quick Intro To JRuby

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 endend

Page 21: Quick Intro To JRuby

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

Page 22: Quick Intro To JRuby

JI: Interface Conversion

package java.util.concurrent;

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

Page 23: Quick Intro To JRuby

JI: Interface Conversion

class SimpleRubyObject def run puts "hi" endend

callable = Executors.callable(SimpleRubyObject.new)callable.call

http://localhost:4567/code/ji/if_conversion.rb

Page 24: Quick Intro To JRuby

JI: Closure Conversion

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

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

Page 25: Quick Intro To JRuby

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

Page 26: Quick Intro To JRuby

Usage

• Runtime

• Wrapper around Java Code

• Calling From Java

• Utilities

Page 27: Quick Intro To JRuby

JRuby as a Runtime

Page 28: Quick Intro To JRuby

JRuby on Rails

• Wrap Rails app into a war file

• Direct support in Glassfish v3 Prelude

• http://kenai.com

• http://mediacast.sun.com

Page 29: Quick Intro To JRuby

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

Page 30: Quick Intro To JRuby

Wrapping Java Code

Page 31: Quick Intro To JRuby

Swing Applications

• Reduce verbosity

• Simplify event handlers

frame = JFrame.new("Hello Swing")

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

Page 32: Quick Intro To JRuby

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

Page 33: Quick Intro To JRuby

Utilities

Page 34: Quick Intro To JRuby

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

Page 35: Quick Intro To JRuby

Build Utilities

• Rake

• Raven

• Buildr

Page 36: Quick Intro To JRuby

Conclusion

Page 37: Quick Intro To JRuby

Resources

• http://jruby.org

• http://wiki.jruby.org

• http://jruby.kenai.com

• http://jtester.codehaus.org

• http://raven.rubyforge.org

• http://buildr.apache.org