1 / 52

Ruby and the tools

Ruby and the tools. CSCE 740 Software Engineering. Topics Ruby Rails. January 15, 2014. Ruby http :// www.ruby-doc.org/ http :// ruby.about.com/od/tutorialsontheweb/tp/10waysfree.htm https ://www.ruby-lang.org/en/documentation/quickstart/ R ails and Beyond

julio
Download Presentation

Ruby and the tools

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ruby and the tools CSCE 740 Software Engineering • Topics • Ruby • Rails January 15, 2014

  2. Ruby • http://www.ruby-doc.org/ • http://ruby.about.com/od/tutorialsontheweb/tp/10waysfree.htm • https://www.ruby-lang.org/en/documentation/quickstart/ • Rails and Beyond • http://ruby.railstutorial.org/ruby-on-rails-tutorial-book

  3. The Rspec Book 2010 • Latest versions • ruby • ruby-gems • rspec • rspec-rails • cucumber • cucumber-rails • database_cleaner • webrat • selenium • rails

  4. Programming Ruby 1.9 Dave Thomas Read Chapters 1-3 Ruby3 • http://www.ruby-doc.org/

  5. Java  Ruby - Three Pillars of Ruby • Everything is an object. • in Java an integer needs to be boxed to act like an object. • Every operation is a method call on some object and returns a value. • in Java operator overloading is different from method overriding • All programming is metaprogramming. • classes can be added or changed at any time even run time SaaS book – Fox and Patterson

  6. Getting Started • ruby –v • ruby prog.rb // puts “hello world\n” • irb // interactive ruby • http://pragprog.com/titles/ruby3/source_code • riClassName //documentation of ClassName Programming Ruby 1.9 Dave Thomas

  7. 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 • puts “the absolute value of #{num} is #{pos}\n” Prog Ruby 1.9 Dave Thomas

  8. 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 Prog Ruby 1.9 Dave Thomas

  9. Puts_examples • song = 1 • sam = "" • defsam.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” Programming Ruby 1.9 Dave Thomas

  10. Symbols • :rhubarb - is an immutable string whose value is value is itself, well without the colon • :rhubarb.to_s yields “rhubarb” • “rhubarb”.to_sym yields :rhubarb • not a string – string operations cannot be performed on it

  11. Method definition • defsay_goodnight(name) • result = "Good night, " + name • return result • end • # Time for bed... • puts say_goodnight("John-Boy") • puts say_goodnight("Mary-Ellen") Programming Ruby 1.9 Dave Thomas

  12. CSE Linux Labs • 1D39 – 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 IPv4 addr and Hardware=ethernetaddr • “man keyword” online unix documentation

  13. Figure 3.1 Basic Ruby Elements SaaS book – Fox and Patterson

  14. List of Ruby 3 Examples • Getting Started – “hello, Ruby Programmer” • Intro – • Hello1 – defsay_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-elseif-else example • Tutclasses-

  15. google(ruby 1.9 tutorial) • Ruby Programming Language - ruby-lang.org/ • http://www.ruby-doc.org/ (again) • http://pragprog.com/titles/ruby3/source_code (again) • http://pragprog.com/book/ruby3/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/

  16. Arrays and Hashes http://ruby-doc.org/docs/ProgrammingRuby/

  17. Control Structures IF • if count > 10    // body • puts "Try again" • elsif tries == 3    • puts "You lose" • else • puts "Enter a number" • end http://ruby-doc.org/docs/ProgrammingRuby/

  18. Control Structures While • while weight < 100 and numPallets <= 30    • pallet = nextPallet()    • weight += pallet.weight • numPallets += 1 • end http://ruby-doc.org/docs/ProgrammingRuby/

  19. 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 http://ruby-doc.org/docs/ProgrammingRuby/

  20. 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(ε) = {“”}

  21. Regular Expressions Examples • ?

  22. Ruby Regular Expressions Examples

  23. 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'   http://ruby-doc.org/docs/ProgrammingRuby/

  24. 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, " -- " http://ruby-doc.org/docs/ProgrammingRuby/

  25. Blocks • 5.times {  print "*" } • 3.upto(6) {|i|  print i } • ('a'..'e').each {|char| print char } • *****3456abcde http://ruby-doc.org/docs/ProgrammingRuby/

  26. Ruby I/O • Already seen • puts • print • P • On reading • Gets • Iterate over lines of file

  27. while gets            # assigns line to $_    • if /Ruby/          # matches against $_     • print             # prints $_    • end • end • ARGF.each { |line|  print line  if line =~ /Ruby/ } http://ruby-doc.org/docs/ProgrammingRuby/

  28. Classes, Objects etc. • class Song    • def initialize(name, artist, duration)      • @name     = name      • @artist   = artist      • @duration = duration    • end • end • s0 =Song.new http://ruby-doc.org/docs/ProgrammingRuby/

  29. aSong = Song.new("Bicylops", "Fleck", 260) • aSong.inspect • aSong.to_s http://ruby-doc.org/docs/ProgrammingRuby/

  30. Inheritance • class KaraokeSong < Song    • def initialize(name, artist, duration, lyrics)      • super(name, artist, duration)      • @lyrics = lyrics    • end • End • aSong = KaraokeSong.new("My Way", "Sinatra", 225, "And now, the...") • aSong.to_s http://ruby-doc.org/docs/ProgrammingRuby/

  31. Accessing instance variables http://ruby-doc.org/docs/ProgrammingRuby/

  32. Class Song • attr_reader :name, :artist, :duration • … • end http://ruby-doc.org/docs/ProgrammingRuby/

  33. class JavaSong {                     // Java code    • private Duration myDuration;    • public void setDuration(Duration newDuration) { • myDuration = newDuration;    • } • } • class Song    • attr_writer :duration • end http://ruby-doc.org/docs/ProgrammingRuby/

  34. 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 plays. Total #@@plays plays."    • end http://ruby-doc.org/docs/ProgrammingRuby/

  35. Class Methods • class Example • definstMeth                # instance method    • … • end • defExample.classMeth      # class method    • … • end • end http://ruby-doc.org/docs/ProgrammingRuby/

  36. Singletons • class Logger    • private_class_method :new    • @@logger = nil    • defLogger.create • @@logger = new unless @@logger      • @@logger    • end • end http://ruby-doc.org/docs/ProgrammingRuby/

  37. 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.” http://ruby-doc.org/docs/ProgrammingRuby/

  38. Specifying Access • class MyClass • def method1     # default is 'public'          • #...        • end •   protected       # subsequent methods will be 'protected' • def method2     # will be 'protected'          • #...        • end •   private             # subsequent methods will be 'private' • def method3     # will be 'private'          • #...        • end •   public              # subsequent methods will be 'public' • def method4     # and this will be 'public'          • #...        • end • end http://ruby-doc.org/docs/ProgrammingRuby/

  39. September 11–Arrays Intro • a = [ 3.14159, "pie", 99 ] • a.type • a[0] • a[1] • a[2] • a[3] http://ruby-doc.org/docs/ProgrammingRuby/

  40. Ruby 1.9 Buy the book page • http://pragprog.com/book/ruby3/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 … http://ruby-doc.org/docs/ProgrammingRuby/

  41. Rubular site - Regular Expressions • Rubular: a Ruby regular expression editor and tester http://rubular.com/

  42. http://rubular.com/

  43. Fig 3.3 SaaS book - All objects All time SaaS book – Fox and Patterson

  44. book_in_stock • class BookInStock • attr_reader :isbn, :price • def initialize(isbn, price) • @isbn = isbn • @price = Float(price) • end • end Programming Ruby 1.9 & 2.0 - Pickaxe

  45. book_in_stock • b1 = BookInStock.new("isbn1", 3) • puts b1 • b2 = BookInStock.new("isbn2", 3.14) • puts b2 • b3 = BookInStock.new("isbn3", "5.67") • puts b3 Programming Ruby 1.9 & 2.0 - Pickaxe

  46. book_in_stock • class BookInStock • def initialize(isbn, price) • @isbn = isbn • @price = Float(price) • end • def to_s • "ISBN: #{@isbn}, price: #{@price}" • end • end • b1 = BookInStock.new("isbn1", 3) • puts b1 • b2 = BookInStock.new("isbn2", 3.14) • puts b2 • b3 = BookInStock.new("isbn3", "5.67") • puts b3 Programming Ruby 1.9 & 2.0 - Pickaxe

  47. book_in_stock4 • class BookInStock • def initialize(isbn, price) • @isbn = isbn • @price = Float(price) • end • def isbn • @isbn • end • def price • @price • end • # .. • end • book = BookInStock.new("isbn1", 12.34) • puts "ISBN = #{book.isbn}" • puts "Price = #{book.price}" Programming Ruby 1.9 & 2.0 - Pickaxe

  48. book_in_stock5 • class BookInStock • attr_reader :isbn, :price • def initialize(isbn, price) • @isbn = isbn • @price = Float(price) • end • def price=(new_price) • @price = new_price • end • # ... • end • book = BookInStock.new("isbn1", 33.80) • puts "ISBN = #{book.isbn}" • puts "Price = #{book.price}" • book.price = book.price * 0.75 # discount price • puts "New price = #{book.price}" Programming Ruby 1.9 & 2.0 - Pickaxe

  49. book_in_stock6 • class BookInStock • attr_reader :isbn • attr_accessor :price • def initialize(isbn, price) • @isbn = isbn • @price = Float(price) • end • # ... • end • book = BookInStock.new("isbn1", 33.80) • puts "ISBN = #{book.isbn}" • puts "Price = #{book.price}" • book.price = book.price * 0.75 # discount price • puts "New price = #{book.price}" Programming Ruby 1.9 & 2.0 - Pickaxe

  50. book_in_stock7 • class BookInStock • 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 = BookInStock.new("isbn1", 33.80) • puts "Price = #{book.price}" • puts "Price in cents = #{book.price_in_cents}" Programming Ruby 1.9 & 2.0 - Pickaxe

More Related