Ruby and the tools Topics Ruby Rails January 15, 2014 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.
Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
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.
CSE341: Programming Languages Lecture 19 Introduction To Ruby; Dynamic OOP; "Duck Typing" Dan Grossman Fall 2011.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
ISBN Chapter 6 Data Types Character Strings Pattern Matching.
LING 388: Language and Computers Sandiway Fong Lecture 2: 8/23.
Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl Linux editors and commands (e.g.
Guide To UNIX Using Linux Third Edition
Scripting with Ruby What is a scripting language? What is Ruby?
Scripting Languages Chapter 8 More About Regular Expressions.
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.
Ruby (on Rails) CSE 190M, Spring 2009 Week 1. The Players Kelly "Everyday I'm Hustlin' " Dunn Kim "Mouse" Todd Ryan "Papa T" Tucker.
Applications of Regular Expressions BY— NIKHIL KUMAR KATTE 1.
The Ruby Programming Language
Regular Expressions A regular expression defines a pattern of characters to be found in a string Regular expressions are made up of – Literal characters.
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
Introduction to Ruby CS 480/680 – Comparative Languages.
CMSC 330: Organization of Programming Languages Ruby Hash Tables (and other topics)
Introduction to Computing Using Python Regular expressions Suppose we need to find all addresses in a web page How do we recognize addresses?
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.
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Ruby and the tools 740Tools05ClassesObjectsVars Topics Ruby Classes Objects Variables Containers Blocks Iterators Spring 2014 CSCE 740 Software Engineering.
Perl and Regular Expressions Regular Expressions are available as part of the programming languages Java, JScript, Visual Basic and VBScript, JavaScript,
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.
Ruby on Rails. What is Ruby? Programming Language Object-oriented Interpreted.
Trends in Scripting Languages History For me the purpose of life is partly to have joy. Programmers often feel joy when they can concentrate on the creative.
1 C++ Syntax and Semantics, and the Program Development Process.
What is a language? An alphabet is a well defined set of characters. The character ∑ is typically used to represent an alphabet. A string : a finite.
Perl Language Yize Chen CS354. History Perl was designed by Larry Wall in 1987 as a text processing language Perl has revised several times and becomes.
Regular Expression - Intro Patterns that define a set of strings (or, pieces of a string) Not wildcards (similar notion, but different thing) Used by utilities.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Python Functions.
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.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
+ Ruby and other programming Languages Ronald L. Ramos.
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
Scripting with Ruby What is a scripting language? What is Ruby?
CS105 Computer Programming PYTHON (based on CS 11 Python track: lecture 1, CALTECH)
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.
Learning Ruby Classes. Variables Variables in Ruby can contain data of any type. You can use variables in your Ruby programs without any declarations.
An Introduction to Regular Expressions Specifying a Pattern that a String must meet.
Introduction to information systems RUBY dr inż. Tomasz Pieciukiewicz.
CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann.
CMSC330 More Ruby. Last lecture Scripting languages Ruby language –Implicit variable declarations –Many control statements –Classes & objects –Strings.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Presented By P.SRIVIDYA 085D1A0552 Programming Language.
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)
Scope History of Ruby. Where can you use Ruby? General Features.
Classes, Objects, And Variables
Ruby Testing 2, 11/9/2004.
Ruby and the tools 740Tools03RubyRegExpr
Ruby and the tools 740Tools05ClassesObjectsVars
CSCE 740 Software Engineering
Lecture 12 Non-Regular Languages
Presentation transcript:

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

– 2 – CSCE 740 Spring 2014 Ruby   ee.htm ee.htm ee.htm  Rails and Beyond 

– 3 – CSCE 740 Spring 2014 The Rspec Book 2010 Latest versions rubyruby ruby-gemsruby-gems rspecrspec rspec-railsrspec-rails cucumbercucumber cucumber-railscucumber-rails database_cleanerdatabase_cleaner webratwebrat seleniumselenium railsrails

– 4 – CSCE 740 Spring 2014 Read Chapters 1-3 Ruby3 Programming Ruby 1.9 Dave Thomas

– 5 – 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.  classes can be added or changed at any time even run time SaaS book – Fox and Patterson

– 6 – CSCE 740 Spring 2014 Getting Started  ruby –v  ruby prog.rb // puts “hello world\n”  irb// interactive ruby   ri ClassName//documentation of ClassName Programming Ruby 1.9 Dave Thomas

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

– 8 – CSCE 740 Spring 2014 Ruby - Variable Name Conventions  Variable Name Punctuation  name - local variable  $name, $NAME, - globals (never use anyway!) – instance variables  – class variables  Name – class names and constants Prog Ruby 1.9 Dave Thomas

– 9 – CSCE 740 Spring 2014 Puts_examples song = 1 sam = "" def sam.play(a) "duh dum, da dum de dum..." "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 – CSCE 740 Spring 2014 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 – CSCE 740 Spring 2014 Method definition def say_goodnight(name) result = "Good night, " + name result = "Good night, " + name return result return resultend # Time for bed... puts say_goodnight("John-Boy") puts say_goodnight("Mary-Ellen") Programming Ruby 1.9 Dave Thomas

– 12 – CSCE 740 Spring 2014 CSE Linux Labs 1D39 – Combination on secure site1D39 – Combination on secure site CSE home page – login with CEC credentials Computer resources/Computer labs List of Linux workstationsList of Linux workstations IP addresses – machine names Ifconfig // interface config IPv4 addr and Hardware=ethernet addrIfconfig // interface config IPv4 addr and Hardware=ethernet addr “man keyword” online unix documentation“man keyword” online unix documentation

– 13 – CSCE 740 Spring 2014 Figure 3.1 Basic Ruby Elements SaaS book – Fox and Patterson

– 14 – CSCE 740 Spring 2014 List of Ruby 3 Examples 1.Getting Started – “hello, Ruby Programmer” 2.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 3.Tutclasses-

– 15 – CSCE 740 Spring 2014 google(ruby 1.9 tutorial) Ruby Programming Language - ruby-lang.org/Ruby Programming Language - ruby-lang.org/Ruby Programming LanguageRuby Programming Language (again) (again) (again) (again) “Buy the book” pagehttp://pragprog.com/book/ruby3/programming-ruby- 1-9 “Buy the book” pagehttp://pragprog.com/book/ruby3/programming-ruby- 1-9http://pragprog.com/book/ruby3/programming-ruby- 1-9 Regular Expressions (download pdf)pdf Namespaces, Source Files, and Distribution (download pdf)pdf Ruby Library Reference Built-in Classes and Modules (download pdf of the entry for class Array)pdf Standard Library

– 16 – CSCE 740 Spring 2014 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]

– 17 – CSCE 740 Spring 2014 Control Structures IF if count > 10 // body puts "Try again" elsif tries == 3 elsif tries == 3 puts "You lose" else else puts "Enter a number"end

– 18 – CSCE 740 Spring 2014 Control Structures While while weight < 100 and numPallets <= 30 while weight < 100 and numPallets <= 30 pallet = nextPallet() pallet = nextPallet() weight += pallet.weight weight += pallet.weight numPallets += 1end

– 19 – CSCE 740 Spring 2014 More control puts "Danger, Will Robinson" if radiation > 3000 while square < 1000 while square < 1000 square = square*squareend More concisely, but readability?? square = square*square while square <

– 20 – 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 | salternation denotes L(r) U L(s) rsconcatenation 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”}strings are regexpr that match themselves,L(“ab”) = {“ab”} the empty string ε is a regular expr L(ε) = {“”}the empty string ε is a regular expr L(ε) = {“”}

– 21 – CSCE 740 Spring 2014 Regular Expressions Examples ?

– 22 – CSCE 740 Spring 2014 Ruby Regular Expressions Examples

– 23 – CSCE 740 Spring 2014 Ruby Regular Expressions if line =~ /Perl|Python/ 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' line.sub(/Perl/, 'Ruby') # replace first 'Perl' with 'Ruby' line.gsub(/Python/, 'Ruby') # replace every 'Python' with 'Ruby'

– 24 – CSCE 740 Spring 2014 Blocks a = %w( ant bee cat dog elk ) # create an arraya.each { |animal| puts animal } # iterate over the contents Yield – will be discussed next time [ 'cat', 'dog', 'horse' ].each do |animal| [ 'cat', 'dog', 'horse' ].each do |animal| print animal, " -- "

– 25 – CSCE 740 Spring 2014 Blocks 5.times { print "*" }3.upto(6) {|i| print i }('a'..'e').each {|char| print char }*****3456abcde

– 26 – CSCE 740 Spring 2014 Ruby I/O Already seenAlready seen puts print P On readingOn reading Gets Iterate over lines of file

– 27 – CSCE 740 Spring 2014 while gets # assigns line to $_ while gets # assigns line to $_ if /Ruby/ # matches against $_ if /Ruby/ # matches against $_ print # prints $_ print # prints $_ endendARGF.each { |line| print line if line =~ /Ruby/ }

– 28 – CSCE 740 Spring 2014 Classes, Objects etc. class Song class Song def initialize(name, artist, duration) def initialize(name, artist, = = = = = = duration endend s0 =Song.new

– 29 – CSCE 740 Spring 2014 aSong = Song.new("Bicylops", "Fleck", 260)aSong.inspectaSong.to_s

– 30 – CSCE 740 Spring 2014 Inheritance class KaraokeSong < Song class KaraokeSong < Song def initialize(name, artist, duration, lyrics) def initialize(name, artist, duration, lyrics) super(name, artist, duration) super(name, artist, = = lyrics endEnd aSong = KaraokeSong.new("My Way", "Sinatra", 225, " And now, the...") aSong.to_s

– 31 – CSCE 740 Spring 2014 Accessing instance variables class Song def end def end def end

– 32 – CSCE 740 Spring 2014 Class Song attr_reader :name, :artist, :duration …end

– 33 – CSCE 740 Spring 2014 class JavaSong { // Java code class JavaSong { // Java code private Duration myDuration; private Duration myDuration; public void setDuration(Duration newDuration) { myDuration = newDuration; myDuration = newDuration; }} class Song class Song attr_writer :durationend

– 34 – CSCE 740 Spring 2014 class Song class Song = 0 = 0 def initialize(name, artist, duration) def initialize(name, artist, = = = = = = = = 0 end end def play def += += 1 += 1 += 1 "song: plays. Total plays." "song: plays. Total plays." end

– 35 – CSCE 740 Spring 2014 Class Methods class Example def instMeth # instance method def instMeth # instance method …end def Example.classMeth # class method def Example.classMeth # class method …endend

– 36 – CSCE 740 Spring 2014 Singletons class Logger class Logger private_class_method :new private_class_method :new = nil = nil def Logger.create def Logger.create = new unless = new unless endend Logger.create.id» Logger.create.id»

– 37 – 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.”

– 38 – CSCE 740 Spring 2014 Specifying Access class MyClass def method1 # default is 'public' def method1 # default is 'public' #... #... end protected # subsequent methods will be 'protected' protected # subsequent methods will be 'protected' def method2 # will be 'protected' def method2 # will be 'protected' #... #... end private # subsequent methods will be 'private' private # subsequent methods will be 'private' def method3 # will be 'private' def method3 # will be 'private' #... #... end public # subsequent methods will be 'public' public # subsequent methods will be 'public' def method4 # and this will be 'public' def method4 # and this will be 'public' #... #... endend

– 39 – CSCE 740 Spring 2014 September 11–Arrays Intro a = [ , "pie", 99 ]a.typea[0]a[1]a[2]a[3]

– 40 – CSCE 740 Spring 2014 Ruby 1.9 Buy the book page Regular Expressions (download pdf)Regular Expressions (download pdf)pdf Namespaces, Source Files, and Distribution (download pdf)Namespaces, Source Files, and Distribution (download pdf)pdf Built-in Classes and Modules (download pdf of the entry for class Array)Built-in Classes and Modules (download pdf of the entry for class Array)pdf Free Content …Free Content …

– 41 – CSCE 740 Spring 2014 Rubular site - Regular Expressions Rubular: a Ruby regular expression editor and tester 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 \AStart of string \zEnd of string

– 42 – CSCE 740 Spring 2014.Any single character \sAny whitespace character \SAny non-whitespace character \dAny digit \DAny non-digit \w Any word character (letter, number, underscore) \WAny non-word character \bAny 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

– 43 – CSCE 740 Spring 2014 Fig 3.3 SaaS book - All objects All time SaaS book – Fox and Patterson

– 44 – CSCE 740 Spring 2014 book_in_stock class BookInStock attr_reader :isbn, :price attr_reader :isbn, :price def initialize(isbn, price) def initialize(isbn, = = = = Float(price) end endend Programming Ruby 1.9 & Pickaxe

– 45 – CSCE 740 Spring 2014 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 & Pickaxe

– 46 – CSCE 740 Spring 2014 book_in_stock class BookInStock def initialize(isbn, price) def initialize(isbn, = = = = Float(price) end end def to_s def to_s "ISBN: price: "ISBN: price: end endend 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 & Pickaxe

– 47 – CSCE 740 Spring 2014 book_in_stock4 class BookInStock def initialize(isbn, price) def initialize(isbn, = = = = Float(price) end end def isbn end end end end #.. #..end book = BookInStock.new("isbn1", 12.34) puts "ISBN = #{book.isbn}" puts "Price = #{book.price}" Programming Ruby 1.9 & Pickaxe

– 48 – CSCE 740 Spring 2014 book_in_stock5 class BookInStock attr_reader :isbn, :price attr_reader :isbn, :price def initialize(isbn, price) def initialize(isbn, = = = = Float(price) end end def price=(new_price) def = = new_price end 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 & Pickaxe

– 49 – CSCE 740 Spring 2014 book_in_stock6 class BookInStock attr_reader :isbn attr_reader :isbn attr_accessor :price attr_accessor :price def initialize(isbn, price) def initialize(isbn, = = = = Float(price) end 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 & Pickaxe

– 50 – CSCE 740 Spring 2014 book_in_stock7 class BookInStock attr_reader :isbn attr_reader :isbn attr_accessor :price attr_accessor :price def initialize(isbn, price) def initialize(isbn, = = = = Float(price) end end def price_in_cents Integer(price* ) Integer(price* ) end end #... #...end book = BookInStock.new("isbn1", 33.80) puts "Price = #{book.price}" puts "Price in cents = #{book.price_in_cents}" Programming Ruby 1.9 & Pickaxe

– 51 – CSCE 740 Spring 2014 book_in_stock8 class BookInStock attr_reader :isbn attr_reader :isbn attr_accessor :price attr_accessor :price def initialize(isbn, price) def initialize(isbn, = = = = Float(price) end end def price_in_cents Integer(price* ) Integer(price* ) end end def price_in_cents=(cents) def = cents / = cents / end end #... #...end Programming Ruby 1.9 & Pickaxe

– 52 – CSCE 740 Spring 2014 book = BookInStock.new("isbn1", 33.80) puts "Price = #{book.price}" puts "Price in cents = #{book.price_in_cents}" book.price_in_cents = 1234