Download - Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Transcript
Page 1: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Ruby for Java Programmers

Mike Bowler

President, Gargoyle Software Inc.

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 1

Page 2: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Why learn another language?

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 2

Page 3: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

History

Originated in Japan in 1993

Created by Yukihiro “Matz” Matsumoto

First English language book published in 2000

Programming Ruby by Dave Thomas and Andy Hunt (the Pragmatic Programmers)

The catalyst, Ruby on Rails, appeared in 2004

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 3

Page 4: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

The Ruby Language

Smalltalk

PerlLisp

Ruby

Python

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 4

Page 5: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Sample Code

class Sample

def print

for i in 1..3

puts i

end

end

end

sample = Sample.new

sample.print

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 5

Page 6: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Defining Strong/Weak Typing

Strong typing

Objects are of a specific type and will not be converted automatically

Java: “4”/2 results in a compile error

Weak typing

Objects can be converted under the covers at any time

Perl: ‘4’/2 => 2

Ruby is strongly typed

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 6

Page 7: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Early/Late Binding

Early binding

All method invocations must be defined at compile time

Java: foo.getUser()

Late binding

The runtime does not check that a given method exists until an attempt to invoke it

Smalltalk: foo getuser.

Ruby uses late binding

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 7

Page 8: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

The Parts of Ruby

Virtual machine with garbage collection

Standard libraries

Core API

• Base classes and modules, such as String, Array, Symbol, etc.

Standard API

• Additional libraries such as CGI, OpenURI, and REXML

RubyVirtual

Machine

Standard API

Core API

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 8

Page 9: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Inheritance

Animal

garfield

Cat

Object

Class

Module

Inherits fromInstance of

garfield = Cat.new

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 9

Page 10: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

class ZebraCage < Cage

attr_accessor :capacity

@@allCages = Array.new

def initialize maximumZebraCount

@capacity = maximumZebraCount

@@allCages << self

end

private

def clean_cage

# do some stuff here

end

end

cage = ZebraCage.new 10

puts cage.capacity

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 10

Page 11: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Naming

do_something if list.empty?

name.strip

name.strip!

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 11

Page 12: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Syntactic Sugar: Array

list = Array.new

list << 'one' << 'two' << 'three'

list.push('one', 'two', 'three')

list = ['one', 'two', 'three']

list = %w(one two three)

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 12

Page 13: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Syntactic Sugar: Hash

hash = Hash.new

hash[:a] = 4

hash[:b] = 5

hash = {:a => 4, :b => 5}

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 13

Page 14: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Syntactic Sugar: Hash

def dump number, hash

puts hash.keys.sort { |a,b|

a.to_s <=> b.to_s

}

end

dump 30, {:a => 5, :b => 3}

dump 100, :a => 5, :b => 3

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 14

Page 15: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Syntactic Sugar: Regexp

line = $1 if line =~ /^\s*(.+)\s*$/

regex = Regexp.new '^\s*(.+)\s*$'

matchData = regex.match line

line = matchData[1] unless matchData.nil?

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 15

Page 16: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Nil and Null

Java’s null Ruby’s nil

Absence of an object An instance of NilClass

if( a != null ) {...} unless a.nil? {...}

null.toString() -> NPE nil.to_s -> “”

null.getUser() ->Exception in thread "main"

java.lang.NullPointerException

nil.get_user -> NoMethodError: undefined method

‘get_user’ for nil:NilClass

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 16

Page 17: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Message != Method

Customer Object

first_name()

Message Method

Person

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 17

Page 18: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

What if there isn’t a method for the specified message?

Customer Object

search()

method_missing()

Message

Method

Person

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 18

Page 19: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

method_missing Example from ActiveRecord

user = Users.find_by_name(name)

user = Users.find(:first,

:conditions => [ "name = ?", name])

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 19

Page 20: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Creating Proxy Objects

Mock object for testing

Proxy object to allow distributed objects across machines

Wrapper to record usage of a given object

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 20

Page 21: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Implementing a Proxy

class Proxy

def method_missing name, *args, &proc

puts name,args

end

end

Proxy.new.foo_bar ‘a’

foo_bar

a

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 21

Page 22: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Implementing a Proxy

class Proxy

instance_methods.each do |method|

undef_method method unless method =~ /^__/

end

def method_missing name, *args, &proc

puts name,args

end

end

Proxy.new.to_s

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 22

Page 23: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Duck Typing

Duck typing implies that an object is interchangeable with any other object that implements the same interface, regardless of whether the objects have a related inheritance hierarchy. – Wikipedia

"If it walks like a duck and quacks like a duck, it must be a duck." – Pragmatic Dave Thomas

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 23

Page 24: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Numeric Overflow: Java

int a = Integer.MAX_VALUE;

System.out.println(" a="+a);

System.out.println("a+1="+(a+1));

a=2147483647

a+1=-2147483648

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 24

Page 25: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Numeric Overflow: Ruby

number = 1000

1.upto(4) do

puts "#{number.class} #{number}"

number = number * number

end

Fixnum 1000

Fixnum 1000000

Bignum 1000000000000

Bignum 1000000000000000000000000

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 25

Page 26: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Closures

An object that is a block of code

Can be assigned to variables and passed to methods

Has access to the scoped variables at the point it is created

number = 1000

1.upto(4) do

puts "#{number.class} #{number}"

number = number * number

end

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 26

Page 27: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Closures

def foo &proc

proc.call 3

proc.call 2

proc.call 1

end

a = 2

foo { |number|

puts number*a

}

foo do | number |

puts number*a

endResults

6

4

2

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 27

Page 28: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Closures – Cleaner Syntax

def foo &proc

proc.call 3

proc.call 2

proc.call 1

end

def foo

yield 3

yield 2

yield 1

end

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 28

Page 29: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Closuresfile = File.new(fileName,'w')

begin

file.puts ‘some content’

rescue

file.close

end

File.open(fileName,'w') do |file|

file.puts ‘some content’

end

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 29

Page 30: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

// Read the lines and split them into columnsList<String[]> lines= new ArrayList<String[]>();BufferedReader reader = null;try { reader = new BufferedReader(new FileReader("people.txt")); String line = reader.readLine(); while( line != null ) { lines.add( line.split("\t") ); }}finally { if( reader != null ) { reader.close(); }}

// then sortCollections.sort(lines, new Comparator<String[]>() { public int compare(String[] one, String[] two) { return one[1].compareTo(two[1]); }});

// then write them back outBufferedWriter writer = null;try { writer = new BufferedWriter( new FileWriter("people.txt") ); for( String[] strings : lines ) { StringBuilder builder = new StringBuilder(); for( int i=0; i<strings.length; i++ ) { if( i != 0 ) { builder.append("\t"); } builder.append(strings[i]); } }}finally { if( writer != null ) { writer.close(); }}

# Load the datalines = Array.newIO.foreach('people.txt') do |line| lines << line.splitend

# Sort and write it back outFile.open('people.txt', 'w') do |file| lines.sort {|a,b| a[1] <=> b[1]}.each do | array | puts array.join("\t") endend

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 30

Page 31: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Ruby File IO Sample# Load the data

lines = Array.new

IO.foreach('people.txt') do |line|

lines << line.split

end

# Sort and write it back out

File.open('people.txt', 'w') do |file|

lines.sort {|a,b| a[1] <=> b[1]}.each do | array |

puts array.join("\t")

end

end

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 31

Page 32: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Closure-like Things in Java

final String name = getName();

new Thread( new Runnable() {

public void run() {

doSomething(name);

}

}).start();

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 32

Page 33: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Closures for Java 7?

Proposal for closures in Javahttp://www.javac.info/closures-v03.html

<T extends java.io.Closeable, throws E>

void closeAtEnd({T=>void}throws E block, T t) throws E {

try {

block.invoke();

} finally {

try { t.close(); } catch (IOException ex) {}

}

}

closeAtEnd(FileReader in : makeReader()) closeAtEnd(FileWriter out : makeWriter()) {

// do something

}

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 33

Page 34: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Inheriting Behaviour from Multiple Places

C++ has multiple inheritance

Java has interfaces

Ruby has mixins

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 34

Page 35: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

C++ : Multiple Inheritance

Class B

Class C

Class A

Class D

Class E

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 35

Page 36: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Java : Inheritance

Class A

Class B

Class C

Interface 1

Interface 1

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 36

Page 37: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Ruby : Mixins

Class A

Mixin 1

Class B

Mixin 1

Mixin 2

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 37

Page 38: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Mixins

module SampleModule

def do_something

puts 'doing something'

end

end

class SampleClass

include SampleModule

end

SampleClass.new.do_something

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 38

Page 39: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Enumerable

Requires that the class implement each()

For max, min and sort the <=> operator is also needed

Adds many methods for modifying, searching, sorting the items

all?, any?, collect, detect, each_cons, each_slice, each_with_index, entries, enum_cons, enum_slice, enum_with_index, find, find_all, grep, include?, inject, map, max, member?, min, partition, reject, select, sort, sort_by, to_a, to_set, zip

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 39

Page 40: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Enumerable Examples

people.find { |person| person.last_name == 'Smith' }

people.sort {|a,b| a.last_name <=> b.last_name}

lastNames = people.collect {|p| p.last_name}

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 40

Page 41: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Domain Specific Languages

A domain-specific programming language (domain-specific language, DSL) is a programming language designed to be useful for a specific set of tasks.– Wikipedia

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 41

Page 42: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

DSL Samples

ANT<target name="compile" depends="init" description="compile" >

<!-- Compile the java code from ${src} into ${build} -->

<javac srcdir="${src}" destdir="${build}"/>

</target>

Google Mapspizza near keystone resort, keystone co

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 42

Page 43: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

DSL Samples

task :clobber => [:clean] do

rm_r "tempdir"

end

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 43

Page 44: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

DSL: ActiveRecord

class ListItem < ActiveRecord::Base belongs_to :amazon_item acts_as_taggable acts_as_list :scope => :userend

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 44

Page 45: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Meta Programming

attr_accessor

has_many

once_only

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 45

Page 46: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

attr_accessor

class Foo

attr_accessor :bar

end

class Foo

def bar

@bar

end

def bar=(newBar)

@bar = newBar

end

end

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 46

Page 47: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Possible Implementation of attr accessor

class Foo

def self.my_attr_accessor symbol

name = symbol.id2name

module_eval <<-DONE

def #{name}()

@#{name}

end

def #{name}=(newValue)

@#{name} = newValue

end

DONE

end

my_attr_accessor :bar

end

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 47

Page 48: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

ActiveRecord

class ListItem < ActiveRecord::Base

belongs_to :amazon_item

acts_as_taggable

acts_as_list :scope => :user

end

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 48

Page 49: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Date :: once

def once(*ids) # :nodoc:

for id in ids

module_eval <<-"end;", __FILE__, __LINE__

alias_method :__#{id.to_i}__, :#{id.to_s}

private :__#{id.to_i}__

def #{id.to_s}(*args, &block)

if defined? @__#{id.to_i}__

@__#{id.to_i}__

elsif ! self.frozen?

@__#{id.to_i}__ ||= __#{id.to_i}__(*args, &block)

else

__#{id.to_i}__(*args, &block)

end

end

end;

end

end

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 49

Page 50: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Continuations

Imagine...

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 50

Page 51: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Continuations

A snapshot of the call

stack that the application

can revert to at some

point in the future

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 51

Page 52: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Continuation Web Server

class SampleServlet < ContinuationServlet

def do_work

for i in 1..35

yield_now if i % 5 == 0

@response.body << " : #{i}"

end

yield_now

@response.body = 'done'

end

end

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 52

Page 53: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Webrick

class Sample < WEBrick::HTTPServlet::AbstractServlet

def service(request, response)

@response.body = “hello world”

end

end

server = WEBrick::HTTPServer.new(:Port => 9005)

server.mount('/', Sample)

trap("INT") { server.shutdown }

server.start

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 53

Page 54: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

class ContinuationException < Exception

end

class ContinuationServlet < WEBrick::HTTPServlet::AbstractServlet

@@cc = nil

def yield_now

@request, @response = callcc do | cc |

@@cc = cc

raise ContinuationException.new

end

end

def service(request, response)

@@cc.call [request, response] unless @@cc.nil?

@request, @response = request, response

begin

do_work()

rescue ContinuationException => e

end

end

end

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 54

Page 55: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Continuation Web Server

class SampleServlet < ContinuationServlet

def do_work

for i in 1..35

yield_now if i % 5 == 0

@response.body << " : #{i}"

end

yield_now

@response.body = 'done'

end

end

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 55

Page 56: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Recap

Learning a new language will make you better with all the languages you know

Ruby has a much more concise syntax which means that it takes much less code to solve the same problems

Concepts

Closures

Domain specific languages

Metaprogramming

Continuations

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 56

Page 57: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Ruby on the Java™ Platform

JRuby

See Roberto Chinnici’s presentation on “Scripting in the Java™ Platform”

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 57

Page 58: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

More Information

Ruby homepage

http://www.ruby-lang.org/en/

Ruby doc

Extensive documentation for all core and standard libraries

http://www.ruby-doc.org/

Ruby Central

Home of the one-click Windows installer

http://www.rubycentral.com/

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 58

Page 59: Ruby for Java Programmers - Software Summit · // Read the lines and split them into columns List lines= new ArrayList(); BufferedReader reader = null;

Contacting Me

Mike Bowler

[email protected]

I’ll be here all week

Please fill out your evaluations

Colorado Software Summit: October 22 – 27, 2006 © Copyright 2006, Gargoyle Software Inc.

Mike Bowler — Ruby for Java Programmers Page 59