Download presentation
Presentation is loading. Please wait.
1
Ruby and the tools 740Tools03RubyRegExpr
CSCE 740 Software Engineering Ruby and the tools 740Tools03RubyRegExpr Topics Ruby Reg Expressions January 22, 2014
2
Ruby Starting-up Rails and Beyond
3
Ruby.new 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” Variable Name Punctuation name - local variable $name, $NAME, - globals (never use anyway!) @name – instance variables – class variables Name – class names and constants Prog Ruby 1.9 Dave Thomas
4
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
5
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
6
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
7
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-
8
google(ruby 1.9 tutorial)
Ruby Programming Language - ruby-lang.org/ (again) (again) “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
9
Programming Ruby 1.9 Dave Thomas
"gin joint".length 9 "Rick".index("c") 2 -1942.abs 1942 sam.play(aSong) "duh dum, da dum de dum ..." Programming Ruby 1.9 Dave Thomas
10
Programming Ruby 1.9 Dave Thomas
def sayGoodnight(name) result = "Goodnight, " + name return result end # Time for bed... puts sayGoodnight("John-Boy") puts sayGoodnight("Mary-Ellen") Programming Ruby 1.9 Dave Thomas
11
Programming Ruby 1.9 Dave Thomas
Simplifying def sayGoodnight(name) result = "Goodnight, #{name}“ return result end #Simplifying further eliminating the return statement Programming Ruby 1.9 Dave Thomas
12
instSection['bassoon'] Programming Ruby 1.9 Dave Thomas
Arrays and Hashes a = [ 1, 'cat', 3.14 ] # array with three elements a[0] >> 1 a[2] = nil #dump array puts a >> [1, "cat", nil] empty1 = [] empty2 = Array.new a = %w{ ant bee cat dog elk } Programming Ruby 1.9 Dave Thomas
13
Programming Ruby 1.9 Dave Thomas
Hashes instSection = { 'cello' => 'string', 'clarinet' => 'woodwind', 'drum' => 'percussion', 'oboe' => 'woodwind', 'trumpet' => 'brass', 'violin' => 'string’ } In the online text “>>” means evaluates to instSection['oboe'] >> instSection['cello'] >> instSection['bassoon'] >> Programming Ruby 1.9 Dave Thomas
14
Programming Ruby 1.9 Dave Thomas
histogram = Hash.new(0) histogram['key1'] = histogram['key1'] + 1 Programming Ruby 1.9 Dave Thomas
15
Control Structures IF if count > 10 // body puts "Try again"
elsif tries == 3 puts "You lose" else puts "Enter a number" end
16
Control Structures While
while weight < 100 and numPallets <= 30 pallet = nextPallet() weight += pallet.weight numPallets += 1 end
17
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
18
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(ε) = {“”}
19
Ruby Regular Expressions
[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
20
Ruby Regular Expressions
(...) 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
21
Quoting First, always remember that you need to escape any of these characters with a backslash if you want them to be treated as regular characters to match: show_regexp('yes | no', /\|/) # => yes ->|<- no show_regexp('yes (no)', /\(no\)/) # => yes ->(no)<- show_regexp('are you sure?', /e\?/) # => are you sur->e?<-
22
Matching Strings with Patterns
Ruby operator =~ matches a string against a pattern. It returns the character offset into the string at which the match occurred: /cat/ =~ "dog and cat" # => 8 /cat/ =~ "catch" # => 0 /cat/ =~ "Cat" # => nil You can put the string first if you prefer:2 "dog and cat" =~ /cat/ # => 8 "catch" =~ /cat/ # => 0 "Cat" =~ /cat/ # => nil
23
Regular Expressions Examples
24
Using Match results in Control Flow
str = "cat and dog" if str =~ /cat/ puts "There's a cat here somewhere" end testing when a pattern does not match a string using !~: File.foreach("testfile").with_index do |line, index| puts "#{index}: #{line}" if line !~ /on/ produces: 1: This is line two 2: This is line three
25
Changing Strings with Patterns
str = "Dog and Cat" new_str1 = str.sub(/a/, "*") new_str2 = str.gsub(/a/, "*") puts "Using sub: #{new_str1}" puts "Using gsub: #{new_str2}“ produces: Using sub: Dog *nd Cat Using gsub: Dog *nd C*t
26
Grouping You can use parentheses to group terms within a regular expression. Everything within the group is treated as a single regular expression. # This matches an 'a' followed by one or more 'n's show_regexp('banana', /an+/) # => b->an<-ana # This matches the sequence 'an' one or more times show_regexp('banana', /(an)+/) # => b->anan<-a
27
Grouping to save portions that match
/(\d\d):(\d\d)(..)/ =~ "12:50am" # => 0 "Hour is #$1, minute #$2" # => "Hour is 12, minute 50" /((\d\d):(\d\d))(..)/ =~ "12:50am" # => 0 "Time is #$1" # => "Time is 12:50" "Hour is #$2, minute #$3" # => "Hour is 12, minute 50" "AM/PM is #$4" # => "AM/PM is am"
28
show_regexp def show_regexp(string, pattern)
match = pattern.match(string) if match "#{match.pre_match}->#{match[0]}<-#{match.post_match}" else "no match" end
29
show_regexp Examples show_regexp('very interesting', /t/)
show_regexp('Fats Waller', /a/) # => F->a<-ts Waller show_regexp('Fats Waller', /lle/) # => Fats Wa->lle<-r show_regexp('Fats Waller', /z/) # => no match
30
Anchors Pattern description ^ match the beginning of a line $
match the end of a line \A matches the beginning of a string \z and \Z Match the end of a string \Z matches the end of a string unless the string ends with \n; it matches just before \b and \B match word boundaries and nonword boundaries
31
Anchor Regular Expressions Examples
str = "this is\nthe time" show_regexp(str, /^the/) # => this is\n->the<- time show_regexp(str, /is$/) # => this ->is<-\nthe time show_regexp(str, /\Athis/) # => ->this<- is\nthe time show_regexp(str, /\Athe/) # => no match show_regexp("this is\nthe time", /\bis/) show_regexp("this is\nthe time", /\Bis/) # => th->is<- is\nthe time
32
Character classes character class - [characters] matches any single character between the brackets For example [aeiou] [cat] = [tac] negated class - [^xyz] matches any single character not x, y or z show_regexp('Price $12.', /[aeiou]/) # => Pr->i<-ce $12. show_regexp('Price $12.', /[\s]/) # => Price-> <-$12. show_regexp('Price $12.', /[$.]/) # => Price ->$<-12.
33
Character class Examples
a = 'see [The PickAxe-page 123]' show_regexp(a, /[A-F]/) # => see [The Pick->A<-xe-page 123] show_regexp(a, /[A-Fa-f]/) # => s->e<-e [The PickAxe-page 123] show_regexp(a, /[0-9]/) # => see [The PickAxe-page ->1<-23] show_regexp(a, /[0-9][0-9]/) # => see [The PickAxe-page ->12<-3]
34
Character class abbreviations
Sequence As [ ... ] Meaning \d [0-9] ASCII decimal digit character \D [^0-9] Any character except a digit \h [0-9a-fA-F] Hexadecimal digit character \H [^0-9a-fA-F] Any character except a hex digit \s [ \t\r\n\f] ASCII whitespace character \S [^ \t\r\n\f] Any character except whitespace \w [A-Za-z0-9\_] ASCII word character \W [^A-Za-z0-9\_] Any character except a word character
35
Ruby Regular Expr Changing text
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'
36
Regexp Constructor Regexps are created using the /.../ and
%r{...} literals, “r” for raw don’t cook the ‘/’ and by the Regexp::new constructor.
37
the %r{...} syntax The %r syntax is particularly useful when creating patterns that contain forward slashes: %r{} is equivalent to the /.../ notation, but allows you to have '/' in your regexp without having to escape them: %r{/home/user} is equivalent to /\\/home\\/user/ /mm\/dd/ # => /mm\/dd/ Regexp.new("mm/dd") # => /mm\/dd/ %r{mm/dd} # => /mm\/dd/
38
Regular Expressions Examples
date = "12/25/2010" date =~ %r{(\d+)(/|:)(\d+)(/|:)(\d+)} [$1,$2,$3,$4,$5] # => ["12", "/", "25", "/", "2010"] date =~ %r{(\d+)(?:/|:)(\d+)(?:/|:)(\d+)} [$1,$2,$3] # => ["12", "25", "2010"]
39
Backslash Sequences in Substitutions
puts "fred:smith".sub(/(\w+):(\w+)/, '\2, \1') puts "nercpyitno".gsub(/(.)(.)/, '\2\1') produces: smith, fred encryption More use of backslashes \& (last match), \+ (last matched group), \‘ (string prior to match), \’ (string after match), and \\ (a literal backslash).
40
str = 'a\b\c' # => "a\b\c" str
str = 'a\b\c' # => "a\b\c" str.gsub(/\\/, '\\\\\\\\') # => "a\\b\\c“ However, using the fact that \& is replaced by the matched string, you could also write this: str.gsub(/\\/, '\&\&') # => "a\\b\\c" If you use the block form of gsub, the string for substitution is analyzed only once : str.gsub(/\\/) { '\\\\' } # => "a\\b\\c"
41
Backslashes in Patterns
same = "12:15-12:45" differ = "12:45-13:15“ # use numbered backreference same =~ /(\d\d):\d\d-\1:\d\d/ # => 0 differ =~ /(\d\d):\d\d-\1:\d\d/ # => nil
42
Cases /pat/i # i for ignore case /\.(gif|jpg|jpeg|png)$/i def mixed_case(name) name.downcase.gsub(/\b\w/) {|first| first.upcase } end mixed_case("DAVE THOMAS") # => "Dave Thomas" mixed_case("dave thomas") # => "Dave Thomas" mixed_case("dAvE tHoMas") # => "Dave Thomas"
43
Lookahead and Lookbehind
44
Rubular 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
45
(. <month>\d{1,2})\/(. <day>\d{1,2})\/(
(?<month>\d{1,2})\/(?<day>\d{1,2})\/(?<year>\d{4}) Today's date is: 1/21/2014.
46
Regular Expression Options
i Case insensitive. The pattern match will ignore the case of letters in the pattern and string. o Substitute once. Any #{...} substitutions in a particular regular expression literal will be performed just once, the first time it is evaluated. Otherwise, the substitutions will be performed every time the literal generates a Regexp object. m Multiline mode. Normally, “.” matches any character except a newline.With the /m option, “.” matches any character. x Extended mode. Complex regular expressions can be difficult to read. The x option allows you to insert spaces and newlines in the pattern to make it more readable. You can also use # to introduce comments.
47
BLOCKS
48
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, " -- "
49
{ puts "Hello" } # this is a block do # club
{ puts "Hello" } # this is a block do # club.enroll(person) # and so is this person.socialize end
50
Blocks 5.times { print "*" } 3.upto(6) {|i| print i }
('a'..'e').each {|char| print char } *****3456abcde
51
def callBlock yield end callBlock { puts "In the block" } Produces In the block
52
Ruby I/O Already seen On reading line = gets print line puts print P
Gets reads line from stdin variable $_ Iterate over lines of file line = gets print line
53
Processing stdin = ARGF
while gets # assigns line to $_ if /Ruby/ # matches against $_ print # prints $_ end Now the “ruby way” ARGF.each { |line| print line if line =~ /Ruby/ }
54
Classes, Objects, and Variables
class Song def initialize(name, artist, duration) @name = name @artist = artist @duration = duration end aSong = Song.new("Bicylops", "Fleck", 260)
55
aSong = Song.new("Bicylops", "Fleck", 260)
aSong.inspect >> aSong.to_s >> "#<Song:0x401b499c>”
56
New improved to_s class Song def to_s “Song: -- } ( } )” end aSong = Song.new("Bicylops", "Fleck", 260) aSong.to_s >> “Song: Bicylops--Fleck (260)”
57
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 …
58
overriding to_s class KarokeSong def to_s “KS: -- } ( } ) end class KaraokeSong < Song super + "
59
Accessing instance variables
Class Song attr_reader :name, :artist, :duration attr_writer :duration … end
60
class JavaSong { // Java code private Duration myDuration;
public void setDuration(Duration newDuration) { myDuration = newDuration; } class Song attr_writer :duration end aSong = Song.new("Bicylops", "Fleck", 260) aSong.duration = 257
61
def initialize(name, artist, duration) @name = name @artist = artist
class Song def initialize(name, artist, duration) @name = name @artist = artist @duration = duration @plays = 0 end def play @plays += 1 end
62
Implementing a SongList Container
append( aSong ) » list Append the given song to the list. deleteFirst() » aSong Remove the first song from the list, returning that song. deleteLast() » aSong Remove the last song from the list, returning that song. [ anIndex } » aSong Return the song identified by anIndex, which may be an integer index or a song title.
63
SongList: Initializer & append
# Initializer class SongList def = Array.new end end #append method class SongList def self
64
SongList: class SongList def end def end
65
SongList: [ ] method 1rst version
class SongList def [ ](key) if else # ... end end end
66
Class Variables class Song = 0 def initialize(name, artist, = = = = 0 end def += 1 += 1 "This song: plays. Total plays. end
67
Class Methods class Example def instMeth # instance method … end
def Example.classMeth # class method
68
Singletons class Logger private_class_method :new @@logger = nil
def Logger.create end Logger.create.id
69
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.”
70
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'
71
Ruby 1.9 “Buy the book page”
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 … More on reg expr
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.