quick intro to jruby

Post on 15-May-2015

3.353 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

A (Quick) Introduction to JRuby

Frederic Jeanfred@fredjean.net

Hello 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

Ruby

Ruby is...

• Dynamically Typed

• Strongly Typed

• Everything is An Object

(Almost) Everything is an Object

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

Symbols

• String-like construct

• Only one copy in memory

:symbol

Blocks

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

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

Ruby Classes

class Person attr_accessor :name, :titleend

p = Person.new

Open Classes

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

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

Mixins

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

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

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

JRuby

JRuby

• Ruby Implementation on the JVM

• Compatible with Ruby 1.8.6

• Native Multithreading

• Full Access to Java Libraries

JRuby Hello World

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

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

Java Integration

require 'java'

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

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

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

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

JI: Interface Conversion

package java.util.concurrent;

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

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

JI: Closure Conversion

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

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

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

Usage

• Runtime

• Wrapper around Java Code

• Calling From Java

• Utilities

JRuby as a Runtime

JRuby on Rails

• Wrap Rails app into a war file

• Direct support in Glassfish v3 Prelude

• http://kenai.com

• http://mediacast.sun.com

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

Wrapping Java Code

Swing Applications

• Reduce verbosity

• Simplify event handlers

frame = JFrame.new("Hello Swing")

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

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

Utilities

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

Build Utilities

• Rake

• Raven

• Buildr

Conclusion

Resources

• http://jruby.org

• http://wiki.jruby.org

• http://jruby.kenai.com

• http://jtester.codehaus.org

• http://raven.rubyforge.org

• http://buildr.apache.org

top related