Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 24 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
24
Dung lượng
127 KB
Nội dung
General Purpose Language General Purpose Language Ruby Ruby Kit Chan Kit Chan COMP3351 COMP3351 Programming Languages Programming Languages November 9, 2007 November 9, 2007 Outline Outline Reference Reference Introduction Introduction Ruby is Ruby is Features Features Array Array Loop Loop Ruby v.s. Java Ruby v.s. Java Duck Typing Duck Typing Dynamic Typing Dynamic Typing Type Tag Checking Type Tag Checking Functional Programming Functional Programming Reference Reference Wikipedia Wikipedia http://en.wikipedia.org/wiki/Ruby_(programming_language) http://en.wikipedia.org/wiki/Ruby_(programming_language) http://en.wikipedia.org/wiki/Comparison_of_programming_languages http://en.wikipedia.org/wiki/Comparison_of_programming_languages Ruby Official Website Ruby Official Website http://www.ruby-lang.org/en/ http://www.ruby-lang.org/en/ Learning Ruby Learning Ruby http://www.math.umd.edu/~dcarrera/ruby/0.3/index.html http://www.math.umd.edu/~dcarrera/ruby/0.3/index.html Duck Typing Duck Typing http://rubylearning.com/satishtalim/duck_typing.html http://rubylearning.com/satishtalim/duck_typing.html Introduction Introduction Developed by Yukihiro “Matz” Matsumoto, in Feb, 1993 Developed by Yukihiro “Matz” Matsumoto, in Feb, 1993 First released in 1995 First released in 1995 1.8.6 Ruby 1.9 developed March, 2007 1.8.6 Ruby 1.9 developed March, 2007 Named Named As a gemstone because of a joke with in Matsumoto’s friends alluding to As a gemstone because of a joke with in Matsumoto’s friends alluding to Perl’s name Perl’s name Ruby is Ruby is Cross-platform Operating System Cross-platform Operating System Combines syntax by Combines syntax by Perl Perl Smalltalk-like OO features Smalltalk-like OO features Shares features with Shares features with Lisp Lisp Python Python Dylan Dylan CLU CLU Ruby is Ruby is The interpreted scripting language for The interpreted scripting language for Quick Quick Easy OO programming Easy OO programming designed for programmer productivity designed for programmer productivity Straight-forward Straight-forward principle of least surprise (POLS) principle of least surprise (POLS) Ruby behaves in a way that minimizing confusion for Ruby behaves in a way that minimizing confusion for experienced users experienced users After you learn Ruby very well After you learn Ruby very well Ruby is Ruby is Obedient Obedient >> 'Hello World' >> 'Hello World' => "Hello World” => "Hello World” >> 'blink ' * 4 >> 'blink ' * 4 => "blink blink blink blink " => "blink blink blink blink " Your Calculator Your Calculator >> 1+1 >> 1+1 => 2 => 2 Or Or >> 2**2**2 >> 2**2**2 => 16 => 16 Features Features Object-oriented Object-oriented Four levels of variables: Four levels of variables: Global Global $var $var Instance Instance @var @var Local Local [a-z] or _; var [a-z] or _; var Constant Constant [A-Z] [A-Z] Exception handling Exception handling Iterators & closures Iterators & closures Automatic garbage collecting Automatic garbage collecting Highly portable Highly portable http://tryruby.hobix.com/ http://tryruby.hobix.com/ Array Array >> numbers = ["zero", "one", "two", "three", "four"] >> numbers = ["zero", "one", "two", "three", "four"] => ["zero", "one", "two", "three", "four"] => ["zero", "one", "two", "three", "four"] => Array => Array >> numbers[0] >> numbers[0] => "zero" => "zero" What arrays do? What arrays do? >> numbers[0].class >> numbers[0].class => String => String >> numbers[0].upcase >> numbers[0].upcase => "ZERO" => "ZERO" >> numbers[0].reverse >> numbers[0].reverse => "orez" => "orez" Loop Loop If I knew Ruby when I was in grade school……. If I knew Ruby when I was in grade school……. >> 100.times do >> 100.times do puts "I won't do that again" puts "I won't do that again" end end I won't do that again I won't do that again => 100 => 100 My life was going to be much easier My life was going to be much easier [...]... 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... 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... 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.. .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 . General Purpose Language General Purpose Language Ruby Ruby Kit Chan Kit Chan COMP3351 COMP3351 Programming Languages Programming Languages November 9, 2007 November. Reference Reference Wikipedia Wikipedia http://en.wikipedia.org/wiki /Ruby_ (programming _language) http://en.wikipedia.org/wiki /Ruby_ (programming _language) http://en.wikipedia.org/wiki/Comparison_of_programming_languages http://en.wikipedia.org/wiki/Comparison_of_programming_languages Ruby Official Website Ruby Official Website http://www .ruby- lang.org/en/ http://www .ruby- lang.org/en/ Learning Ruby Learning Ruby http://www.math.umd.edu/~dcarrera /ruby/ 0.3/index.html http://www.math.umd.edu/~dcarrera /ruby/ 0.3/index.html Duck. seconds"); } } Ruby v.s. Java Ruby v.s. Java Performance Performance Ruby: 24 – 26 seconds Ruby: 24 – 26 seconds Java: 1 – 2 seconds Java: 1 – 2 seconds Language Language Paradigm Paradigm Type