CSCE 740 Software Engineering

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Introduction to Ruby.
Advertisements

JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Python Objects and Classes
John Pinney Ruby in 20 minutes John Pinney
Language of the Month If it’s December, it must be Ruby! Adam Coffman and Brent Beer.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
ISBN Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl –(on reserve.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
ISBN Chapter 6 Data Types Character Strings Pattern Matching.
Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl Linux editors and commands (e.g.
Introduction to Perl Software Tools. Slide 2 Introduction to Perl l Perl is a scripting language that makes manipulation of text, files, and processes.
Guide To UNIX Using Linux Third Edition
Scripting with Ruby What is a scripting language? What is Ruby?
CSE467/567 Computational Linguistics Carl Alphonce Computer Science & Engineering University at Buffalo.
CSC 9010: Natural Language Processing
Ruby (on Rails) CSE 190M, Spring 2009 Week 4. Constructors Writing a new class is simple! Example: class Point end But we may want to initialize state.
Ruby (on Rails) Slides modified by ements-2ed.shtml) 1.
The Ruby Programming Language
Introduction to Ruby CS 480/680 – Comparative Languages.
CMSC 330: Organization of Programming Languages Ruby Hash Tables (and other topics)
Ruby and the tools Topics Ruby Rails January 15, 2014 CSCE 740 Software Engineering.
Python: Classes By Matt Wufsus. Scopes and Namespaces A namespace is a mapping from names to objects. ◦Examples: the set of built-in names, such as the.
Ruby and the tools 740Tools05ClassesObjectsVars Topics Ruby Classes Objects Variables Containers Blocks Iterators Spring 2014 CSCE 740 Software Engineering.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
COMP 3438 – Part II - Lecture 2: Lexical Analysis (I) Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ. 1.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Introduction to sed. Sed : a “S tream ED itor ” What is Sed ?  A “non-interactive” text editor that is called from the unix command line.  Input text.
+ Ruby and other programming Languages Ronald L. Ramos.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
Scripting with Ruby What is a scripting language? What is Ruby?
Ruby Objects, Classes and Variables CS 480/680 – Comparative Languages.
Scripting with Ruby What is a scripting language? What is Ruby?
Standard Types and Regular Expressions CS 480/680 – Comparative Languages.
Introduction to information systems RUBY dr inż. Tomasz Pieciukiewicz.
CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann.
Presented By P.SRIVIDYA 085D1A0552 Programming Language.
Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:
Regular Expressions Copyright Doug Maxwell (
Chapter 2 Writing Simple Programs
Strings and Serialization
Ruby Getting started & Ruby.New Presented by Mario Tayah
Ruby: An Introduction Created by Yukihiro Matsumoto in 1993 (named after his birthstone) Pure OO language (even the number 1 is an instance of a class)
Python: Experiencing IDLE, writing simple programs
Scope History of Ruby. Where can you use Ruby? General Features.
Introduction to Python
Chapter 3 Lexical Analysis.
© 2016 Pearson Education, Ltd. All rights reserved.
Introduction Python is an interpreted, object-oriented and high-level programming language, which is different from a compiled one like C/C++/Java. Its.
Java Primer 1: Types, Classes and Operators
Regular Expression - Intro
Objectives Identify the built-in data types in C++
Programming Language Concepts (CIS 635)
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
Classes, Objects, And Variables
Chapter 14 Introduction to Ruby.
Alan Borning Spring 2018 (slides borrowed from Dan Grossman)
Java Programming Language
Ruby Testing 2, 11/9/2004.
Chapter 1: Computer Systems
Ruby and the tools 740Tools03RubyRegExpr
Ruby and the tools 740Tools05ClassesObjectsVars
Python Primer 1: Types and Operators
CISC101 Reminders All assignments are now posted.
Lecture 12 Non-Regular Languages
Introduction to Computer Science
Classes, Objects and Methods
Code Blocks, Closures, and Continuations
Learning Python 5th Edition
Presentation transcript:

CSCE 740 Software Engineering Ruby and the tools 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/ Rails and Beyond http://ruby.railstutorial.org/ruby-on-rails-tutorial-book

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

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

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

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

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

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

Programming Ruby 1.9 Dave Thomas 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” Programming Ruby 1.9 Dave Thomas

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

Programming Ruby 1.9 Dave Thomas 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") Programming Ruby 1.9 Dave Thomas

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=ethernet addr “man keyword” online unix documentation

Figure 3.1 Basic Ruby Elements SaaS book – Fox and Patterson

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

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/

Arrays and Hashes a = [ 1, 'cat', 3.14 ] # array with three elements # access the first element a[0] » 1 # set the third element a[2] = nil # dump out the array a [1, "cat", nil] http://ruby-doc.org/docs/ProgrammingRuby/

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/

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

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/

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

Regular Expressions Examples ?

Ruby Regular Expressions Examples

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/

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/

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

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

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

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

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

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/

Accessing instance variables class Song   def name     @name   end   def artist     @artist   def duration     @duration end http://ruby-doc.org/docs/ProgrammingRuby/

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

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/

def initialize(name, artist, duration) @name = name @artist = artist 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/

Class Methods class Example def instMeth # instance method … end    def Example.classMeth      # class method    http://ruby-doc.org/docs/ProgrammingRuby/

Singletons class Logger private_class_method :new @@logger = nil def Logger.create      @@logger = new unless @@logger      @@logger    end Logger.create.id » 537766930 http://ruby-doc.org/docs/ProgrammingRuby/

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/

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

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/

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/

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-zA-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 http://rubular.com/

. 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 http://rubular.com/

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

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

Programming Ruby 1.9 & 2.0 - Pickaxe 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

Programming Ruby 1.9 & 2.0 - Pickaxe book_in_stock class BookInStock def initialize(isbn, price) @isbn = isbn @price = Float(price) end def to_s "ISBN: #{@isbn}, price: #{@price}" 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

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

Programming Ruby 1.9 & 2.0 - Pickaxe book_in_stock5 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}" class BookInStock attr_reader :isbn, :price def initialize(isbn, price) @isbn = isbn @price = Float(price) end def price=(new_price) @price = new_price # ... Programming Ruby 1.9 & 2.0 - Pickaxe

Programming Ruby 1.9 & 2.0 - Pickaxe book_in_stock6 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}" class BookInStock attr_reader :isbn attr_accessor :price def initialize(isbn, price) @isbn = isbn @price = Float(price) end # ... Programming Ruby 1.9 & 2.0 - Pickaxe

Programming Ruby 1.9 & 2.0 - Pickaxe book_in_stock7 def price_in_cents Integer(price*100 + 0.5) end # ... book = BookInStock.new("isbn1", 33.80) puts "Price = #{book.price}" puts "Price in cents = #{book.price_in_cents}" class BookInStock attr_reader :isbn attr_accessor :price def initialize(isbn, price) @isbn = isbn @price = Float(price) end Programming Ruby 1.9 & 2.0 - Pickaxe

Programming Ruby 1.9 & 2.0 - Pickaxe book_in_stock8 def price_in_cents Integer(price*100 + 0.5) end def price_in_cents=(cents) @price = cents / 100.0 # ... class BookInStock attr_reader :isbn attr_accessor :price def initialize(isbn, price) @isbn = isbn @price = Float(price) end Programming Ruby 1.9 & 2.0 - Pickaxe

book = BookInStock. new("isbn1", 33. 80) puts "Price = #{book book = BookInStock.new("isbn1", 33.80) puts "Price = #{book.price}" puts "Price in cents = #{book.price_in_cents}" book.price_in_cents = 1234