e5981e929b7a454aac8bdd1a2557d8ef.ppt
- Количество слайдов: 52
CSCE 740 Software Engineering Ruby and the tools Topics n n January 15, 2014 Ruby Rails
Ruby § http: //www. ruby-doc. org/ § http: //ruby. about. com/od/tutorialsontheweb/tp/10 waysfr ee. htm § https: //www. ruby-lang. org/en/documentation/quickstart/ Rails and Beyond § http: //ruby. railstutorial. org/ruby-on-rails-tutorial-book – 2 – CSCE 740 Spring 2014
The Rspec Book 2010 Latest versions • ruby-gems • rspec-rails • cucumber-rails • database_cleaner • webrat • selenium • rails – 3 – CSCE 740 Spring 2014
Read Chapters 1 -3 Ruby 3 • http: //www. ruby-doc. org/ – 4 – Programming Ruby 1. 9 Dave Thomas CSCE 740 Spring 2014
Java Ruby - Three Pillars of Ruby 1. Everything is an object. § in Java an integer needs to be boxed to act like an object. 2. Every operation is a method call on some object and returns a value. § in Java operator overloading is different from method overriding 3. All programming is metaprogramming. § – 5 – classes can be added or changed at any time even run time Saa. S book – Fox and Patterson CSCE 740 Spring 2014
Getting Started § ruby –v § ruby prog. rb // puts “hello worldn” § irb // interactive ruby § http: //pragprog. com/titles/ruby 3/source_code § ri Class. Name – 6 – //documentation of Class. Name Programming Ruby 1. 9 Dave Thomas CSCE 740 Spring 2014
Ruby is Really Object Oriented § Really Object Oriented – everything is an object § For Java you might use pos = Math. abs(num) § In Ruby num = -123 pos = num. abs § Variables inside literal strings #{ … } notation § – 7 – puts “the absolute value of #{num} is #{pos}n” Prog Ruby 1. 9 Dave Thomas CSCE 740 Spring 2014
Ruby - Variable Name Conventions § Variable Name Punctuation § name - local variable § $name, $NAME, - globals (never use anyway!) § @name – instance variables § @@name – class variables § Name – class names and constants – 8 – Prog Ruby 1. 9 Dave Thomas CSCE 740 Spring 2014
Puts_examples song = 1 sam = "" def sam. play(a) "duh dum, da dum de dum. . . " end puts "gin joint". length puts "Rick". index("c") puts 42. even? puts sam. play(song) print “string with no newline” – 9 – Programming Ruby 1. 9 Dave Thomas CSCE 740 Spring 2014
Symbols : rhubarb - is an immutable string whose value is itself, well without the colon : rhubarb. to_s yields “rhubarb”. to_sym yields : rhubarb not a string – string operations cannot be performed on it – 10 – CSCE 740 Spring 2014
Method definition def say_goodnight(name) result = "Good night, " + name return result end # Time for bed. . . puts say_goodnight("John-Boy") puts say_goodnight("Mary-Ellen") – 11 – Programming Ruby 1. 9 Dave Thomas CSCE 740 Spring 2014
CSE Linux Labs • 1 D 39 – Combination on secure site • CSE home page – login with CEC credentials • Computer resources/Computer labs • List of Linux workstations • IP addresses – machine names • Ifconfig // interface config IPv 4 addr and Hardware=ethernet addr • “man keyword” online unix documentation – 12 – CSCE 740 Spring 2014
Figure 3. 1 Basic Ruby Elements – 13 – Saa. S book – Fox and Patterson CSCE 740 Spring 2014
List of Ruby 3 Examples 1. Getting Started – “hello, Ruby Programmer” 2. Intro – a. b. c. d. e. f. Hello 1 – def say_goodnight Puts examples Cmd_line – command line args passed into ruby program “arrays” – non-homogeneous hash_with_symbol_keys_19. rb – Weekdays – control structures – if-else example 3. Tutclasses- – 14 – CSCE 740 Spring 2014
google(ruby 1. 9 tutorial) • Ruby Programming Language - ruby-lang. org/ • http: //www. ruby-doc. org/ (again) • http: //pragprog. com/titles/ruby 3/source_code (again) • http: //pragprog. com/book/ruby 3/programming-ruby-1 -9 “Buy the book” page • • • Regular Expressions (download pdf) Namespaces, Source Files, and Distribution (download pdf) Ruby Library Reference • Built-in Classes and Modules (download pdf of the entry for class Array) • Standard Library • http: //www. ruby-doc. org/stdlib-1. 9. 3/ – 15 – CSCE 740 Spring 2014
Arrays and Hashes a = [ 1, 'cat', 3. 14 ] # array with three elements # access the first element a[0] » 1 » [1, "cat", nil] # set the third element a[2] = nil # dump out the array a – 16 – http: //ruby-doc. org/docs/Programming. Ruby/ CSCE 740 Spring 2014
Control Structures IF if count > 10 puts "Try again" elsif tries == 3 puts "You lose" else puts "Enter a number" end – 17 – // body http: //ruby-doc. org/docs/Programming. Ruby/ CSCE 740 Spring 2014
Control Structures While weight < 100 and num. Pallets <= 30 pallet = next. Pallet() weight += pallet. weight num. Pallets += 1 end – 18 – http: //ruby-doc. org/docs/Programming. Ruby/ CSCE 740 Spring 2014
More control puts "Danger, Will Robinson" if radiation > 3000 while square < 1000 square = square*square end More concisely, but readability? ? square = square*square while square < 1000 – 19 – http: //ruby-doc. org/docs/Programming. Ruby/ CSCE 740 Spring 2014
Regular Expressions Regular expressions are expressions that specify collections of strings (formally languages) Main operators: Assume r and s are regular expressions then so are: r | s alternation denotes L(r) U L(s) rs concatenation which denotes L(r) L(s) r* Kleene closure zero or more occurrences of strings from L(r) concatenated Base regular expressions • strings are regexpr that match themselves, L(“ab”) = {“ab”} • the empty string ε is a regular expr L(ε) = {“”} – 20 – CSCE 740 Spring 2014
Regular Expressions Examples • ? – 21 – CSCE 740 Spring 2014
Ruby Regular Expressions Examples – 22 – CSCE 740 Spring 2014
Ruby Regular Expressions if line =~ /Perl|Python/ puts "Scripting language mentioned: #{line}” end line. sub(/Perl/, 'Ruby') # replace first 'Perl' with 'Ruby' line. gsub(/Python/, 'Ruby') # replace every 'Python' with 'Ruby' – 23 – http: //ruby-doc. org/docs/Programming. Ruby/ CSCE 740 Spring 2014
Blocks a = %w( ant bee cat dog elk ) # create an array a. each { |animal| puts animal } # iterate over the contents Yield – will be discussed next time [ 'cat', 'dog', 'horse' ]. each do |animal| print animal, " -- " – 24 – http: //ruby-doc. org/docs/Programming. Ruby/ CSCE 740 Spring 2014
Blocks 5. times { print "*" } 3. upto(6) {|i| print i } ('a'. . 'e'). each {|char| print char } *****3456 abcde – 25 – http: //ruby-doc. org/docs/Programming. Ruby/ CSCE 740 Spring 2014
Ruby I/O • Already seen • puts • print P • • On reading • • – 26 – Gets Iterate over lines of file CSCE 740 Spring 2014
while gets if /Ruby/ print end # assigns line to $_ # matches against $_ # prints $_ ARGF. each { |line| print line if line =~ /Ruby/ } – 27 – http: //ruby-doc. org/docs/Programming. Ruby/ CSCE 740 Spring 2014
Classes, Objects etc. class Song def initialize(name, artist, duration) @name = name @artist = artist @duration = duration end s 0 =Song. new – 28 – http: //ruby-doc. org/docs/Programming. Ruby/ CSCE 740 Spring 2014
a. Song = Song. new("Bicylops", "Fleck", 260) a. Song. inspect a. Song. to_s – 29 – http: //ruby-doc. org/docs/Programming. Ruby/ CSCE 740 Spring 2014
Inheritance class Karaoke. Song < Song def initialize(name, artist, duration, lyrics) super(name, artist, duration) @lyrics = lyrics end End a. Song = Karaoke. Song. new("My Way", "Sinatra", 225, " And now, the. . . ") a. Song. to_s – 30 – http: //ruby-doc. org/docs/Programming. Ruby/ CSCE 740 Spring 2014
Accessing instance variables class Song def name @name end def artist @artist end def duration @duration end – 31 – http: //ruby-doc. org/docs/Programming. Ruby/ CSCE 740 Spring 2014
Class Song attr_reader : name, : artist, : duration … end – 32 – http: //ruby-doc. org/docs/Programming. Ruby/ CSCE 740 Spring 2014
class Java. Song { // Java code private Duration my. Duration; public void set. Duration(Duration new. Duration) { my. Duration = new. Duration; } } class Song attr_writer : duration end – 33 – http: //ruby-doc. org/docs/Programming. Ruby/ CSCE 740 Spring 2014
class Song @@plays = 0 def initialize(name, artist, duration) @name = name @artist = artist @duration = duration @plays = 0 end def play @plays += 1 @@plays += 1 "song: #@plays. Total #@@plays. " end – 34 – http: //ruby-doc. org/docs/Programming. Ruby/ CSCE 740 Spring 2014
Class Methods class Example def inst. Meth # instance method … end def Example. class. Meth # class method … end – 35 – http: //ruby-doc. org/docs/Programming. Ruby/ CSCE 740 Spring 2014
Singletons class Logger private_class_method : new @@logger = nil def Logger. create @@logger = new unless @@logger end Logger. create. id – 36 – » » 537766930 http: //ruby-doc. org/docs/Programming. Ruby/ CSCE 740 Spring 2014
Access Control “Public methods can be called by anyone---there is no access control. Methods are public by default (except for initialize, which is always private). Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. Private methods cannot be called with an explicit receiver. Because you cannot specify an object when using them, private methods can be called only in the defining class and by direct descendents within that same object. ” – 37 – http: //ruby-doc. org/docs/Programming. Ruby/ CSCE 740 Spring 2014
Specifying Access class My. Class def method 1 # default is 'public' #. . . end protected # subsequent methods will be 'protected' def method 2 # will be 'protected' #. . . end private # subsequent methods will be 'private' def method 3 # will be 'private' #. . . end public # subsequent methods will be 'public' def method 4 # and this will be 'public' #. . . end CSCE 740 Spring 2014 – 38 – end http: //ruby-doc. org/docs/Programming. Ruby/
September 11–Arrays Intro a = [ 3. 14159, "pie", 99 ] a. type a[0] a[1] a[2] a[3] – 39 – http: //ruby-doc. org/docs/Programming. Ruby/ CSCE 740 Spring 2014
Ruby 1. 9 Buy the book page http: //pragprog. com/book/ruby 3/programming-ruby-1 -9 • Regular Expressions (download pdf) • Namespaces, Source Files, and Distribution (download pdf) • Built-in Classes and Modules (download pdf of the entry for class Array) • Free Content … – 40 – http: //ruby-doc. org/docs/Programming. Ruby/ CSCE 740 Spring 2014
Rubular site - Regular Expressions Rubular: a Ruby regular expression editor and tester [abc] A single character of: a, b or c [^abc] Any single character except: a, b, or c [a-z] Any single character in the range a-z [a-z. A-Z] Any single character in the range a-z or A-Z ^ Start of line $ End of line A Start of string z End of string – 41 – http: //rubular. com/ CSCE 740 Spring 2014
. Any single character s Any whitespace character S Any non-whitespace character d Any digit D Any non-digit w Any word character (letter, number, underscore) W Any non-word character b Any word boundary (. . . ) Capture everything enclosed (a|b) a or b a? Zero or one of a a* Zero or more of a a+ One or more of a a{3} Exactly 3 of a a{3, } 3 or more of a a{3, 6} Between 3 and 6 of a – 42 – http: //rubular. com/ CSCE 740 Spring 2014
Fig 3. 3 Saa. S book - All objects All time – 43 – Saa. S book – Fox and Patterson CSCE 740 Spring 2014
book_in_stock class Book. In. Stock attr_reader : isbn, : price def initialize(isbn, price) @isbn = isbn @price = Float(price) end – 44 – Programming Ruby 1. 9 & 2. 0 - Pickaxe CSCE 740 Spring 2014
book_in_stock b 1 = Book. In. Stock. new("isbn 1", 3) puts b 1 b 2 = Book. In. Stock. new("isbn 2", 3. 14) puts b 2 b 3 = Book. In. Stock. new("isbn 3", "5. 67") puts b 3 – 45 – Programming Ruby 1. 9 & 2. 0 - Pickaxe CSCE 740 Spring 2014
book_in_stock class Book. In. Stock def initialize(isbn, price) @isbn = isbn @price = Float(price) end def to_s "ISBN: #{@isbn}, price: #{@price}" b 1 = Book. In. Stock. new("isbn 1", 3) puts b 1 b 2 = Book. In. Stock. new("isbn 2", 3. 14) puts b 2 end b 3 = Book. In. Stock. new("isbn 3", "5. 67") end puts b 3 – 46 – Programming Ruby 1. 9 & 2. 0 - Pickaxe CSCE 740 Spring 2014
book_in_stock 4 class Book. In. Stock def initialize(isbn, price) @isbn = isbn @price = Float(price) end def isbn @isbn end – 47 – def price @price end #. . end book = Book. In. Stock. new("isbn 1", 12. 34) puts "ISBN = #{book. isbn}" puts "Price = #{book. price}" Programming Ruby 1. 9 & 2. 0 - Pickaxe CSCE 740 Spring 2014
book_in_stock 5 class Book. In. Stock attr_reader : isbn, : price def initialize(isbn, price) @isbn = isbn @price = Float(price) end def price=(new_price) @price = new_price book = Book. In. Stock. new("isbn 1", 33. 80) puts "ISBN = #{book. isbn}" puts "Price = #{book. price}" book. price = book. price * 0. 75 # discount price puts "New price = #{book. price}" end #. . . – 48 – end Programming Ruby 1. 9 & 2. 0 - Pickaxe CSCE 740 Spring 2014
book_in_stock 6 class Book. In. Stock attr_reader : isbn attr_accessor : price def initialize(isbn, price) @isbn = isbn @price = Float(price) end book = Book. In. Stock. new("isbn 1", 33. 80) puts "ISBN = #{book. isbn}" puts "Price = #{book. price}" book. price = book. price * 0. 75 # discount price puts "New price = #{book. price}" #. . . end – 49 – Programming Ruby 1. 9 & 2. 0 - Pickaxe CSCE 740 Spring 2014
book_in_stock 7 class Book. In. Stock attr_reader : isbn attr_accessor : price def initialize(isbn, price) @isbn = isbn @price = Float(price) end def price_in_cents Integer(price*100 + 0. 5) end #. . . end book = Book. In. Stock. new("isbn 1", 33. 80) puts "Price = #{book. price}" puts "Price in cents = #{book. price_in_cents}" – 50 – Programming Ruby 1. 9 & 2. 0 - Pickaxe CSCE 740 Spring 2014
book_in_stock 8 class Book. In. Stock attr_reader : isbn def price_in_cents Integer(price*100 + 0. 5) end attr_accessor : price def price_in_cents=(cents) @price = cents / 100. 0 def initialize(isbn, price) end @isbn = isbn #. . . @price = Float(price) end – 51 – Programming Ruby 1. 9 & 2. 0 - Pickaxe CSCE 740 Spring 2014
book = Book. In. Stock. new("isbn 1", 33. 80) puts "Price = #{book. price}" puts "Price in cents = #{book. price_in_cents}" book. price_in_cents = 1234 – 52 – CSCE 740 Spring 2014
e5981e929b7a454aac8bdd1a2557d8ef.ppt