A world outside of Java - Rice...

125
A world outside of Java 1

Transcript of A world outside of Java - Rice...

Page 1: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

A world outside of Java

1

Page 2: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Before we get started

2

Page 3: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Before we get started

A few words on testing

2

Page 4: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Before we get started

A few words on testing

Let’s look at some stats from project 1

2

Page 5: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Testing: coverage

0

25

50

75

100

A B C D E F G H I J

Cov

erag

e (%

)

Statement Branch Loop Strict Condition

3

Page 6: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Testing: passage

60%

40%

All Passed Failures

4

Page 7: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Testing: automation

90%

10%

Automation of Test SuiteSingleton Test Cases

5

Page 8: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Testing: thoughts?

6

Page 9: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Testing: goals

7

Page 10: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Testing: goals

One-click automatic testing of all major functionality

7

Page 11: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Testing: goals

One-click automatic testing of all major functionality

Ideally, 100% coverage of all code

7

Page 12: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Testing: goals

One-click automatic testing of all major functionality

Ideally, 100% coverage of all code

Practically, 70% coverage of statement, loop, and conditions

7

Page 13: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Testing: goals

One-click automatic testing of all major functionality

Ideally, 100% coverage of all code

Practically, 70% coverage of statement, loop, and conditions

Meaningful tests

7

Page 14: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Testing: suggestions

8

Page 15: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Testing: suggestions

Write tests alongside code

Even better, have your partner write them

8

Page 16: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Testing: suggestions

Write tests alongside code

Even better, have your partner write them

Think of corner cases

Verifying basic functionality isn’t enough!

8

Page 17: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Testing: suggestions

Write tests alongside code

Even better, have your partner write them

Think of corner cases

Verifying basic functionality isn’t enough!

Use a code coverage tool

Ask a grader

8

Page 18: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Testing: requirements

9

Page 19: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Testing: requirements

All tests must pass

9

Page 20: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Testing: requirements

All tests must pass

All major functionality must be covered

9

Page 21: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Testing: requirements

All tests must pass

All major functionality must be covered

Create a suite to run all tests

One class, preferably called AllTests

Makes it easier for you to prevent regression

NEW!

9

Page 22: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Testing: JUnit Suites

JUnit 4 JUnit 3

10

Page 23: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Testing: JUnit Suites

JUnit 4 JUnit 3

import org.junit.*;import org.junit.runner.*;import org.junit.runners.*;

@RunWith(value=Suite.class)@Suite.SuiteClasses(value=

UnitTest1.class,UnitTest2.class,IntegrationTest1.class,...

})public class AllTests {}

10

Page 24: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Testing: JUnit Suites

JUnit 4 JUnit 3

import org.junit.*;import org.junit.runner.*;import org.junit.runners.*;

@RunWith(value=Suite.class)@Suite.SuiteClasses(value=

UnitTest1.class,UnitTest2.class,IntegrationTest1.class,...

})public class AllTests {}

import junit.framework.Test;import junit.framework.TestSuite;

public class AllTests {public static Test suite() {

TestSuite suite = new TestSuite("Cool test suite");

suite.addTest(UnitTest1.suite());suite.addTest(UnitTest2.suite())

...return suite;

}}

10

Page 25: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Testing: JUnit Suites

JUnit 4 JUnit 3

Automatically

generated

by Eclipse

import org.junit.*;import org.junit.runner.*;import org.junit.runners.*;

@RunWith(value=Suite.class)@Suite.SuiteClasses(value=

UnitTest1.class,UnitTest2.class,IntegrationTest1.class,...

})public class AllTests {}

import junit.framework.Test;import junit.framework.TestSuite;

public class AllTests {public static Test suite() {

TestSuite suite = new TestSuite("Cool test suite");

suite.addTest(UnitTest1.suite());suite.addTest(UnitTest2.suite())

...return suite;

}}

10

Page 26: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Questions on testing?

11

Page 27: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

The business

12

Page 28: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

An AlternativeThe world outside of Java

13

Page 29: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

An AlternativeThe world outside of Java

13

Page 30: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

An AlternativeThe world outside of Java

13

Page 31: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

An alternative

14

Page 32: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

An alternative

You guys have heard enough about Lisp

14

Page 33: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

An alternative

You guys have heard enough about Lisp

14

Page 34: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

The real deal

15

Page 35: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

The real deal

I get an hour to warp your minds however I wish

15

Page 36: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

The real deal

I get an hour to warp your minds however I wish

It’s time to spread the propaganda joy

15

Page 37: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

The real deal

I get an hour to warp your minds however I wish

It’s time to spread the propaganda joy

One of my guilty pleasures...

15

Page 38: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

The real deal

I get an hour to warp your minds however I wish

It’s time to spread the propaganda joy

One of my guilty pleasures...

Ruby15

Page 39: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

What is Ruby?

An object-oriented scripting language

A more different Perl

Just as powerful

Simpler, better OO

Principle of “least surprise” -- What you expect is what you get

16

Page 40: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Typical uses of Ruby

System scripts (superb regex support)

Web programming (CGI, Rails)

Also agents and crawlers

DB programming (DBI, ActiveRecord)

GUI (Tk, RubyMagick)

Scientific computing (GSL, RSRuby)

17

Page 41: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby philosophy according to Matz

Lightweight Languages are not lightweight in the sense of ease of implementation, but they are called lightweight because of their intention to lighten the workload of the programmer.

18

Page 42: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby philosophy according to Matz

Brevity is one element that makes code beautiful. As Paul Graham says, “Succinctness is power.” In the vocabulary of programming, brevity is a virtue.

Brevity can also mean the elimination of redundancy

In order to eliminate redundancy, we follow the DRY principle: Don’t Repeat Yourself. The concept of DRY is the antithesis of copy-and-paste coding.

19

Page 43: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby philosophy according to Matz

Simplicity is the next element of beautiful code. We often feel beauty in simple code

When simpler tools are used to solve a complex problem, complexity is merely shifted to the programmer.

20

Page 44: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby philosophy according to Matz

Balance is the final element of beautiful code. [I advocate] brevity, conservatism, simplicity, and flexibility. No element by itself will ensure a beautiful program.

21

Page 45: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby philosophy

Enable programmers to write beautiful code

Make their lives easier

You should enjoy coding, not waste time hacking

22

Page 46: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

General Features

High level language

True OO -- EVERYTHING is an object

Interpreted (for the most part)

Easy to learn

23

Page 47: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby as a high level language

24

Page 48: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the high level language

25

Page 49: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the high level language

Interpreted with garbage collected runtime

25

Page 50: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the high level language

Interpreted with garbage collected runtime

Extensive libraries

25

Page 51: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the high level language

Interpreted with garbage collected runtime

Extensive libraries

Very easy to accomplish common tasks and computations

25

Page 52: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the high level language

Interpreted with garbage collected runtime

Extensive libraries

Very easy to accomplish common tasks and computations

Great regexp support

25

Page 53: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the high level language

Interpreted with garbage collected runtime

Extensive libraries

Very easy to accomplish common tasks and computations

Great regexp support

Easy to pick up from other languages

25

Page 54: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the high level language

Simple, straightforward syntax

26

Page 55: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the high level language

Simple, straightforward syntax

`[]/pfmlj4321;qbyurso.65= -kcdtheaz87 'xgvwni,09

26

Page 56: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the high level language

Simple, straightforward syntax

27

Page 57: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the high level language

Simple, straightforward syntax

perl -le ' chomp(@words = sort {length($b) <=> length($a)} grep {/^[a-z\-]{2,}\s*$/} `cat wordlist`); push @words, "a", "i";$source=shift @ARGV; $let{lc $1}++ while $source =~ /([a-z])/ig; @owords{map {lc $_} ($source=~/\b([a-z]+)\b/ig)}=(); $p=join "", map {"$_*"} sort keys %let;@words = grep {!exists $owords{$_}} grep {join("",sort split //, $_) =~ /^$p$/} @words; sub next_word { my $wi=shift;my $l = shift @_; W:for(my $i=$wi;$i<@words;$i++){ my $word=$words[$i];my %t = %$l; foreach my $ll (grep {/[a-z]/} split //, $word) { next W if --$t{$ll} < 0 } if (grep {$_ > 0} values %t){next_word($i,\%t,@_,$word)} else{print join ", ", @_, $word}}};next_word(0,\%let)' "phrase"

27

Page 58: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the high level language

Simple, straightforward syntax

28

Page 59: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the high level language

Simple, straightforward syntax

def anagrams(chars, result='', &b) if chars.empty? then yield result else chars.length.times do |i| c = (x = chars.clone).slice!(i) anagrams(x, result + c, &b) end endend

words = []anagrams(“phrase”){|anagram| words << anagram}

28

Page 60: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the high level language

Iteration of enumerable objects is really easy

29

Page 61: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the high level language

Iteration of enumerable objects is really easy

# Declare an array with some membersxs = [1, 2, ‘a’, 5, 17, :mother, nil]

# print a verbose representation to stdoutxs.each{|x| puts x.inspect}

# same thing in block formxs.each do |x| puts xend

29

Page 62: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the high level language

Mutation of enumerable objects is just as simple

30

Page 63: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the high level language

Mutation of enumerable objects is just as simple

# Declare an array with some numbersxs = [1, 2, 94, 4, 17, 23, 5]

ys = xs.sort.map{|x| x + 1}# ys --> [2, 3, 5, 6, 18, 24, 95]

30

Page 64: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Object orientation in Ruby

31

Page 65: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the OO language

32

Page 66: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the OO language

Starts with the same general features of Java

32

Page 67: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the OO language

Starts with the same general features of Java

Base class Object

32

Page 68: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the OO language

Starts with the same general features of Java

Base class Object

Public, private, and protected visibility

32

Page 69: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the OO language

Starts with the same general features of Java

Base class Object

Public, private, and protected visibility

No multiple inheritance

32

Page 70: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the OO language

Starts with the same general features of Java

Base class Object

Public, private, and protected visibility

No multiple inheritance

Package namespaces

32

Page 71: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the OO language

Starts with the same general features of Java

Base class Object

Public, private, and protected visibility

No multiple inheritance

Package namespaces

Similarity ends there

32

Page 72: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the OO language

33

Page 73: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the OO language

No [anonymous] inner classes

“Poor man’s closures”

33

Page 74: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the OO language

No [anonymous] inner classes

“Poor man’s closures”

Ruby has real closures

33

Page 75: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the OO language

No [anonymous] inner classes

“Poor man’s closures”

Ruby has real closuresclass Adder

attr_reader :add_nattr_accessor :n

def initialize(n)@n = n@add_n = lambda{|x| x + n}

endend

adder = Adder.new(5)add_closure = adder.add_nadd_closure.call(3) # returns 3 + 5adder.n = 8add.closure.call(3) # returns 3 + 8

33

Page 76: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the OO language

34

Page 77: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the OO language

Duck typing, not identity by inheritance

34

Page 78: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the OO language

Duck typing, not identity by inheritance

“If it walks like a duck and quacks like a duck, I would call it a duck.”

34

Page 79: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the OO language

Duck typing, not identity by inheritance

“If it walks like a duck and quacks like a duck, I would call it a duck.”

An object’s current set of methods and properties determines the valid semantics

34

Page 80: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the OO language

Duck typing, not identity by inheritance

“If it walks like a duck and quacks like a duck, I would call it a duck.”

An object’s current set of methods and properties determines the valid semantics

Similar to OCaml structural typing, but type compatibility is determined only by the part of a type’s structure that is accessed

34

Page 81: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: duck typing

Manifestation of Duck Typing

No abstract classes

(Though modules can define classless groups of methods)

No interfaces

instanceof is no longer an appropriate means of introspection

35

Page 82: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: duck typing

We care about object behavior more than inheritance

36

Page 83: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: duck typing

We care about object behavior more than inheritance

class Fee attr_reader :date

def date=(val) if val.class != Date then raise ArgumentError.new(“Not a date”) end endend

36

Page 84: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: duck typing

We care about object behavior more than inheritance

class Fee attr_reader :date

def date=(val) if val.class != Date then raise ArgumentError.new(“Not a date”) end endend

36

Page 85: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: duck typing

We care about object behavior more than inheritancedef date=(val) class="keyword">case val when Date @date = val when Time @date = Date.new(val.year, val.month, val.day) when String if val =~ /(\d{4})\s*[-\/\\]\s*(\d{1,2})\s*[-\/\\]\s*(\d{1,2})/ @date = Date.new($1.to_i,$2.to_i,$3.to_i) else raise ArgumentError, "Unable to parse #{val} as date" end when Array if val.length == 3 @date = Date.new(val[0], val[1], val[2]) end else raise ArgumentError, "Unable to parse #{val} as date" endend

37

Page 86: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: duck typing

We care about object behavior more than inheritancedef date=(val) class="keyword">case val when Date @date = val when Time @date = Date.new(val.year, val.month, val.day) when String if val =~ /(\d{4})\s*[-\/\\]\s*(\d{1,2})\s*[-\/\\]\s*(\d{1,2})/ @date = Date.new($1.to_i,$2.to_i,$3.to_i) else raise ArgumentError, "Unable to parse #{val} as date" end when Array if val.length == 3 @date = Date.new(val[0], val[1], val[2]) end else raise ArgumentError, "Unable to parse #{val} as date" endend

37

Page 87: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: duck typing

We care about object behavior more than inheritance

# Accepts an object which responds to the +year+, +month+ and +day+# methods.def date=(val) [:year, :month, :day].each do |meth| raise ArgumentError unless val.responds_to?(meth) end @date = valend

38

Page 88: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: duck typing

We care about object behavior more than inheritance

# Accepts an object which responds to the +year+, +month+ and +day+# methods.def date=(val) [:year, :month, :day].each do |meth| raise ArgumentError unless val.responds_to?(meth) end @date = valend

This is a pretty good solution

38

Page 89: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the OO language

If this were Java, we’d have to work in many files to separate this new functionality from the original classes

Create DateCastable interface

Add hooks in target classes

Implement DateCaster class

This can mean a lot of work in many files

39

Page 90: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: class extension

The Ruby Way

Methods and fields can be added to classes at any time

Just create a new file with the required operations

One file to implement and another to test

40

Page 91: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: class extension

“Visitor” example Just extend existing Fie, Foe and Fum classes

class Fie # Note we don’t have to inherit from anything def date

... endend

class Foe def date

... endend

class Fun def date

...end

end

41

Page 92: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: class extension

Other examples

Additions to NArray: mean, median, ranks...

Additions to Fixnum: factorial

Additions to ActiveRecord::Base: find_or_create, for_name

42

Page 93: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: mixins

Ruby also has a mechanism to implement generic code

Separate such patterns into modules

Import these methods into classes to create “Mixins”

43

Page 94: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: mixins

Ruby also has a mechanism to implement generic code

Separate such patterns into modules

Import these methods into classes to create “Mixins”

Great way tostay DRY

43

Page 95: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: mixins

44

Page 96: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: mixins

First, let’s look at modules

44

Page 97: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: mixins

First, let’s look at modules

They’re similar to Java abstract classes

Cannot be instantiated

Can fully implement methods

44

Page 98: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: mixins

First, let’s look at modules

They’re similar to Java abstract classes

Cannot be instantiated

Can fully implement methods

One big difference

They’re included, not used as superclasses

44

Page 99: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: mixins

45

Page 100: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: mixins# A Math module akin to Java Math class.module Math def add(val_one, val_two) BigInteger.new(val_one + val_two) endend

45

Page 101: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: mixins# A Math module akin to Java Math class.module Math def add(val_one, val_two) BigInteger.new(val_one + val_two) endend

# Convert a integer value to English.module Stringify # Requires an instance variable @value def stringify if @value == 1 "One" elsif @value == 2 "Two" elsif @value == 3 "Three" end endend

45

Page 102: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: mixins# A Math module akin to Java Math class.module Math def add(val_one, val_two) BigInteger.new(val_one + val_two) endend

# Base Number classclass Number def intValue @value endend

# Convert a integer value to English.module Stringify # Requires an instance variable @value def stringify if @value == 1 "One" elsif @value == 2 "Two" elsif @value == 3 "Three" end endend

45

Page 103: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: mixins# A Math module akin to Java Math class.module Math def add(val_one, val_two) BigInteger.new(val_one + val_two) endend

# Base Number classclass Number def intValue @value endend

# BigInteger extends Numberclass BigInteger < Number

# Add instance methods from Stringify include Stringify

# Add class methods from Math extend Math

# Add a constructor with one parameter def initialize(value) @value = value endend

# Convert a integer value to English.module Stringify # Requires an instance variable @value def stringify if @value == 1 "One" elsif @value == 2 "Two" elsif @value == 3 "Three" end endend

45

Page 104: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: mixins

46

Page 105: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: mixins

# Create a new objectbigint1 = BigInteger.new(10)# Call a method inherited from the base classputs bigint1.intValue # --> 10

46

Page 106: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: mixins

# Create a new objectbigint1 = BigInteger.new(10)# Call a method inherited from the base classputs bigint1.intValue # --> 10

# Call class method extended from Mathbigint2 = BigInteger.add(-2, 4)puts bigint2.intValue # --> 2

46

Page 107: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: mixins

# Create a new objectbigint1 = BigInteger.new(10)# Call a method inherited from the base classputs bigint1.intValue # --> 10

# Call class method extended from Mathbigint2 = BigInteger.add(-2, 4)puts bigint2.intValue # --> 2

# Call a method included from Stringifyputs bigint2.stringify # --> 'Two'

46

Page 108: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: mixins

But wait, there’s more!

You can mixin modules with instances

47

Page 109: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby: mixins

But wait, there’s more!

You can mixin modules with instances

# Add the module methods to this object instancebigint2.extend CurrencyFormatterputs bigint2.format # --> '$2'

puts bigint1.format # will generate an error

47

Page 110: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the interpreted language

48

Page 111: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the interpreter

Languages gain incredible flexibility with managed runtimes

Garbage collection

Dynamic class hierarchies

Code as Data

49

Page 112: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the interpreter

Ruby’s runtime is similar to many others

Interactive interpreter available

Hooks for debuggers

Slower than compiled

50

Page 113: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby the interpreter

Its runtime is probably Ruby’s weakest link

Mark and sweep GC

Green threads

Fairly slow

Each of these is addressed in the development branch

51

Page 114: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

A few more cool things

52

Page 115: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

No Mutation...

...unless you really want it

53

Page 116: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

No Mutation...

...unless you really want it

xs = [1, 2, 3]xs.slice(1) --> 2xs --> [1, 2, 3]xs.slice!(1) --> 2xs --> [1, 3]

53

Page 117: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Web development

Ruby on Rails

Very nice MVC architecture

Great templating engine with AJAX support

Persistent objects

Countless plugins

Authentication, common database tasks, more AJAX stuff, generic code (e.g. pagination)

54

Page 118: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Distributed Ruby

Just like Java’s RMI without the pain

55

Page 119: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Distributed Ruby

Just like Java’s RMI without the pain

require 'drb'

class TestServer def doit "Hello, Distributed World" endend

aServerObject = TestServer.newDRb.start_service('druby://localhost:9000', aServerObject)DRb.thread.join # Don't exit just yet!

55

Page 120: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Distributed Ruby

Just like Java’s RMI without the pain

require 'drb'

class TestServer def doit "Hello, Distributed World" endend

aServerObject = TestServer.newDRb.start_service('druby://localhost:9000', aServerObject)DRb.thread.join # Don't exit just yet!

require 'drb'DRb.start_service()obj = DRbObject.new(nil, 'druby://localhost:9000')# Now use objputs obj.doit

55

Page 121: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

OO design libraries

Visitor pattern: traverse a collection without knowing its internal organization

Delegates: flexible and dynamic class composition

Singleton pattern: ensures only one instance of a class exists at a time

Observer pattern: implements protocol allowing one object to notify a set of interested objects when certain changes have occurred

56

Page 122: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Access to other languages

JRuby

C API

Bridges to specialty languauges

MATLAB, R, etc.

57

Page 123: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

So, that’s about it

58

Page 124: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Ruby is neat

Simple, clear, concise syntax

Advanced OO features

Extensive libraries and bridges to other languages

59

Page 125: A world outside of Java - Rice Universitysys.cs.rice.edu/course/comp314/_/09/lectures/testing_ruby.pdf · Duck typing, not identity by inheritance “If it walks like a duck and quacks

Flames, abuse, and questions

60