General Purpose Language Ruby

Download Report

Transcript General Purpose Language Ruby

General Purpose Language
Ruby
Kit Chan
COMP3351
Programming Languages
November 9, 2007
Outline
Reference
 Introduction
 Ruby is
 Features
 Array
 Loop
 Ruby v.s. Java
 Duck Typing
 Dynamic Typing
 Type Tag Checking
 Functional Programming

Reference

Wikipedia
http://en.wikipedia.org/wiki/Ruby_(programming_language)
http://en.wikipedia.org/wiki/Comparison_of_programming_languages

Ruby Official Website
http://www.ruby-lang.org/en/

Learning Ruby
http://www.math.umd.edu/~dcarrera/ruby/0.3/index.html

Duck Typing
http://rubylearning.com/satishtalim/duck_typing.html
Introduction

Developed by Yukihiro “Matz” Matsumoto, in
Feb, 1993

First released in 1995
1.8.6 Ruby 1.9 developed March, 2007
 Named


As a gemstone because of a joke with in
Matsumoto’s friends alluding to Perl’s name
Ruby is
Cross-platform Operating System
 Combines syntax by

Perl
 Smalltalk-like OO features


Shares features with
Lisp
 Python
 Dylan
 CLU

Ruby is

The interpreted scripting language for
Quick
 Easy OO programming

designed for programmer productivity
 Straight-forward


principle of least surprise (POLS)
 Ruby
behaves in a way that minimizing confusion for
experienced users

After you learn Ruby very well
Ruby is

Obedient
>> 'Hello World'
=> "Hello World”
 >> 'blink ' * 4
 => "blink blink blink blink "

Your Calculator
>> 1+1
=> 2
Or
>> 2**2**2
=> 16
Features
Object-oriented
 Four levels of variables:

Global
 Instance
 Local
 Constant

$var
@var
[a-z] or _; var
[A-Z]
Exception handling
 Iterators & closures
 Automatic garbage collecting
 Highly portable
http://tryruby.hobix.com/

Array





>> numbers = ["zero", "one", "two", "three", "four"]
=> ["zero", "one", "two", "three", "four"]
=> Array
>> numbers[0]
=> "zero"
What arrays do?






>> numbers[0].class
=> String
>> numbers[0].upcase
=> "ZERO"
>> numbers[0].reverse
=> "orez"
Loop

If I knew Ruby when I was in grade school…….
>> 100.times do
 .. puts "I won't do that again"
 .. end


I won't do that again





=> 100
 My life was going to be much easier

Ruby v.s. Java- Syntax


















begin_time = Time.now.to_i
i=0
100.times do
i += 1
j=0
10.times do
j += 1
k=0
100.times do
k += 1
puts i.to_s + " + " + j.to_s + " + " + k.to_s
end
end
end
end_time = Time.now.to_i
difference = end_time - begin_time
puts "It took " + difference.to_s + " seconds"
ho = gets

















class test
{
public static void main(String[] args)
{
long startTime = System.currentTimeMillis();
for (int i=0; i<=100 ; i++ )
{
for (int j=0; j<=10 ; j++)
{
for (int k=0; k<=100 ; k++ )
{
System.out.println( i + " + " + j + " + " + k);}}}
long endTime = System.currentTimeMillis();
long difference = (endTime - startTime)/1000;
System.out.println("It took " + difference + " seconds");
}
}
Ruby v.s. Java

Performance


Ruby: 24 – 26 seconds
Java: 1 – 2 seconds
Language
Paradigm
Type
Checking
Java
Imperative
Static
Object-oriented
Generic
Ruby
Imperative
Object-oriented
Functional
Aspect-oriented
Dynamic
(duck)
Duck Typing

Ruby interpreter is happy to treat it as it were a
duck


If an object walks and talks like a duck
Duck typing means
An object type is defined by what it can do
 Not by what it is


Duck typing refers to
less concerned with the class of an object
 more concerned with what methods can be called
on it
 what operations can be performed

Duck Typing

we use respond_to?

Example






>> puts ('A string'.respond_to? :to_str)
true
=> nil
>> puts (Exception.new.respond_to? :to_str)
true
=> nil
If an object quacks like a duck, just treat it as a
duck….
 We should treat objects to the methods they defin

Duck Typing

>> class Duck

>> class Goose

.. def quack

.. def honk

.. 'Quack!'

.. 'Honk!'

.. end

.. end

.. def swim

.. def swim

.. 'Paddle paddle paddle...'

.. 'Splash splash splash...'

.. end

.. end

.. end

.. end

=> nil

=> nil

>>
Duck Typing

>> class DuckRecording



.. def quack




.. play
.. end





.. def play

.. 'Quack'

.. end

.. end


=> nil










>> def make_it_quack(duck)
.. duck.quack
.. end
=> nil
>> puts make_it_quack(Duck.new)
Quack!
>> puts make_it_quack(DuckRecording.new)
Quack
=> nil
>> def make_it_swim(duck)
.. duck.swim
.. end
=> nil
>> puts make_it_swim(Duck.new)
Paddle paddle paddle...
=> nil
>> puts make_it_swim(Goose.new)
Splash splash splash...
=> nil
Add Method to Class Instances

Add methods to individual class instances

class Duck
def quack
puts 'Quack!'
end
def swim
puts 'Paddle paddle paddle...'
end
end
d = Duck.new #create new instance of the class
d.quack
#call method
d.swim
#call method
def d.walk
#override existing method with
#new functionality
puts 'I am walking ... walking'
end
























d.walk
=> nil
=> #<Duck:0x6cc7ddc>
Quack!
=> nil
Paddle paddle paddle...
=> nil
=> nil
I am walking ... walking
=> nil
irb(main):022:0>
Dynamic Typing

Ruby, the data types are not wholly declared on
variable

Data associated with variables are not known
until the time of execution

Advantage:
flexibility
 Less work for the programmer

Type Tag Checking

Ruby is dynamically typed


it supports run-time dispatch on tagged data
Takes place at run-time

values bound in variables can acquire different tags
depending on the execution path
Example
var x
 x :=1
 x :=“hi”


……………illegal


binding x to values of inconsistent type
Pure Dynamically typed system allows the
execution


#declares the name x
#associates int val 1 to name x
#associates the string val “hi” to name x
Type tags are attached to values
Dynamic typing catches errors during program
execution
Example......cont

Dynamic typing keeps all program values
“tagged”

Checks the tag before using any value in an
operation
var x :=1
 var y := “hi”
 var z := x + y

#binds val 1 to x
#binds val “hi” to y
#add x to y
Example......cont

The value bound to x be a pair (integer, 1)

The value bound to y be a pair (string, “hi”)

Attempts to execute the 3rd line,
Checks the type tags integer and string
 If the operation + (addition) is not defined

 An
error is signaled
Why Ruby?

High productivity for programmers


Web Developments


Execution time is not the main concern
Projects like Ruby on Rails
Functional Programming

Paradigm treats computation as the evaluation of
mathematical functions
 Emphasize

on application of functions
Largely being used in academia
 Lambda

calculus
Forms the foundation for most models of functional programming
Thank you !!