The Case for Dynamic Languages

Sam Ruby

Case for Dynamic Languages

a.k.a.

Reinventing Smalltalk, one decade at a time

Preface

What do these have in common?

Hints

Preface

So… what do these have in common?
Answer:

Comparison

Printing out all arguments in uppercase in a few languages:

Perl
print join(' ',map {uc} @ARGV) . "\n";
Python
print ' '.join(map(string.upper,sys.argv[1:]))
Ruby
puts ARGV.map{|arg| arg.upcase}.join(' ')

To minimize distraction: I’m going to focus primarily on one (Ruby)

Part I

Personal retrospective

BASIC

10 print "Enter your name:" 
20 input a$
30 print "Hello, "; a$; "!" 

1970's

1980's

1990's

2000's

Other activities

2010's?

Part II

Meanwhile…

C derived languages

SmallTalk

Smalltalk

Source: wikipedia

Everything is an object

Integer.parseInt(“5”)
“5”.to_i
Math.abs(-5)
-5.abs

type-checking is dynamic

def dump(results) results << "abc" results << "xyz" end
Examples of classes that implement ”<<” :

message-sending

Facilities enabled by being able to capture messages:

everything is modifyable

Facilities enabled by being able to modify existing classes:

MVC(Model View Controller)

Ruby on Rails

Ruby on Rails

Part V

Blocks and Continuations

Block Example

sorted_employees = employees.sort_by {|e| e.lastname}

Block Usages

Ensuring post process

Scanner sc = new Scanner(new File(args[0])); try { while (sc.hasNextLine()) { System.out.println(sc.nextLine()); } } finally { sc.close(); }
open(ARGV[0]) do |f| f.readlines.each {|line| puts line} end

Closure Example

def high_paid_employees(employees, limit) employees.collect{|e| e.salary > limit} end

Part III

Incredible shrinking code

Hello World!

class Greeting { public static void main(String args[]) { System.out.println(“Hello World!”); } }
Kernel::puts(“Hello World!”);

Hello World!

class Greeting { public static void main(String args[]) { System.out.println(“Hello World!”); } }
puts(“Hello World!”);

Hello World!

class Greeting { public static void main(String args[]) { System.out.println(“Hello World!”); } }
puts(“Hello World!”)

Hello World!

class Greeting { public static void main(String args[]) { System.out.println(“Hello World!”); } }
puts “Hello World!”

Beans

class Point { private int x; public int getX() { return this.x; } public void setX(int x) { this.x = x; } }

Beans

class Point def x() return @x end def x=(x) @x=x end end

Beans

class Point { private float x; public float getX() { return this.x; } public void setX(float x) { this.x = x; } }
class Point def x() @x end def x=(x) @x=x end end

Beans

class Point { private float x; public float getX() { return this.x; } public void setX(float x) { this.x = x; } }
class Point attr_accessor :x end

Beans

class Point { private float x,y; public float getX() { return this.x; } public void setX(float x) { this.x = x; } public float getY() { return this.y; } public void setY(float y) { this.y = y; } }
class Point attr_accessor :x, :y end

Rails

class Entry < ActiveRecord::Base acts_as_tree :order => “updated” validates_presence_of :updated end

Exceptions

Scanner sc = new Scanner(new File(args[0])); while (sc.hasNextLine()) { System.out.println(sc.nextLine()); } sc.close();

Exceptions

Scanner sc = new Scanner(new File(args[0])); try { while (sc.hasNextLine()) { System.out.println(sc.nextLine()); } } finally { sc.close(); }

Exceptions

f = open(ARGV[0]) begin for line in f.readlines puts line end ensure f.close end f.close();

Exceptions

open(ARGV[0]) do |f| f.readlines.each {|line| puts line} end

Summary

Dynamic languages

References