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