MTAT Agile Software Development in the Cloud Lecture 4: More on Ruby

Similar documents
MTAT Agile Software Development in the Cloud. Lecture 3: Ruby (cont.)

No variable declara<ons. sum = 0! i = 1! while i <= 10 do! sum += i*i! i = i + 1! end! print "Sum of squares is #{sum}\n"!

Ruby Regular Expressions AND FINITE AUTOMATA

CS Unix Tools & Scripting

Ruby: Introduction, Basics

CSE 413 Programming Languages & Implementation. Hal Perkins Winter 2019 Ruby Containers, Blocks, and Procs

Regular Expressions. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 9

Ruby: Introduction, Basics

Computing Unit 3: Data Types

Learning Ruby. Regular Expressions. Get at practice page by logging on to csilm.usu.edu and selecting. PROGRAMMING LANGUAGES Regular Expressions

Lecture 3. Miscellaneous Ruby and Testing

Indian Institute of Technology Kharagpur. PERL Part III. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. I.I.T.

CSE 413 Final Exam. June 7, 2011

More Scripting and Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

Ruby: Introduction, Basics

Paolo Santinelli Sistemi e Reti. Regular expressions. Regular expressions aim to facilitate the solution of text manipulation problems

CSE 413 Final Exam Spring 2011 Sample Solution. Strings of alternating 0 s and 1 s that begin and end with the same character, either 0 or 1.

CS160A EXERCISES-FILTERS2 Boyd

Writing a Lexer. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, February 6, Glenn G.

Chapter 1. Fundamentals of Higher Order Programming

Lexical Analysis. Lecture 3-4

Configuring the RADIUS Listener LEG

Regex, Sed, Awk. Arindam Fadikar. December 12, 2017

Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

One can evaluate any valid string as code at run time using eval.

Understanding Regular Expressions, Special Characters, and Patterns

CSE au Final Exam Sample Solution

Programming Paradigms

CS 5142 Scripting Languages

正则表达式 Frank from

Introduction to String Manipulation

A Brief Introduction to Scheme (I)

CS Unix Tools. Fall 2010 Lecture 5. Hussam Abu-Libdeh based on slides by David Slater. September 17, 2010

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Ruby Regular Expressions

Describing Languages with Regular Expressions

Pieter van den Hombergh. April 13, 2018

CMSC 330: Organization of Programming Languages. Ruby Regular Expressions

CS 211 Regular Expressions

Configuring the RADIUS Listener Login Event Generator

CSE 401 Midterm Exam Sample Solution 2/11/15

UNIX / LINUX - REGULAR EXPRESSIONS WITH SED

Control flow statements

Regular Expressions. Michael Wrzaczek Dept of Biosciences, Plant Biology Viikki Plant Science Centre (ViPS) University of Helsinki, Finland

Introduction to Ruby. SWEN-250 Personal Software Engineering

Ruby : A Dynamic Object Oriented Scripting Language

Server-side Web Development (I3302) Semester: 1 Academic Year: 2017/2018 Credits: 4 (50 hours) Dr Antoun Yaacoub

Regular Expressions. Regular expressions are a powerful search-and-replace technique that is widely used in other environments (such as Unix and Perl)

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

STREAM EDITOR - REGULAR EXPRESSIONS

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Bashed One Too Many Times. Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009

Pattern Matching. An Introduction to File Globs and Regular Expressions

Regular Expressions. Regular Expression Syntax in Python. Achtung!

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

SCHEME AND CALCULATOR 5b

PIC 16: Iterators and Generators

Pattern Matching. An Introduction to File Globs and Regular Expressions. Adapted from Practical Unix and Programming Hunter College

The SPL Programming Language Reference Manual

MUTABLE LISTS AND DICTIONARIES 4

Intro. Scheme Basics. scm> 5 5. scm>

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

MTAT Agile Software Development

CSE341 Autumn 2017, Final Examination December 12, 2017

Outline CS4120/4121. Compilation in a Nutshell 1. Administration. Introduction to Compilers Andrew Myers. HW1 out later today due next Monday.

Regex Guide. Complete Revolution In programming For Text Detection

CSE341 Spring 2017, Final Examination June 8, 2017

CSC 121: Computer Science for Statistics

CMSC330 Fall 2014 Midterm 1 Grading

Classes and Objects in Ruby

Syntactic Analysis. CS345H: Programming Languages. Lecture 3: Lexical Analysis. Outline. Lexical Analysis. What is a Token? Tokens

Lecture 3. Miscellaneous Ruby and Testing 1 / 40

file:///users/dave/tmp/rubyadv/html/metaprogramming.html

YOLOP Language Reference Manual

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

CS 1301 Exam 1 Fall 2010

Control Structures. Important Semantic Difference

psed [-an] script [file...] psed [-an] [-e script] [-f script-file] [file...]

Perl Programming. Bioinformatics Perl Programming

Installation Download and installation instructions can be found at

The Three Rules. Program. What is a Computer Program? 5/30/2018. Interpreted. Your First Program QuickStart 1. Chapter 1

Regular Expressions Overview Suppose you needed to find a specific IPv4 address in a bunch of files? This is easy to do; you just specify the IP

Practical Questions CSCA48 Week 6

Sample Final Exam Questions

CSE 154 LECTURE 11: REGULAR EXPRESSIONS

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Structure of Programming Languages Lecture 3

Functional Programming. Pure Functional Languages

Overview of the Ruby Language. By Ron Haley

Ruby: Useful Classes and Methods

CMSC330 Spring 2015 Midterm #1 9:30am/11:00am/12:30pm

ENGR 101 Engineering Design Workshop


Space War Class Diagram. Elements of OOP. How to design interactions between objects. Space War Class Diagram with Inheritance

Lecture 3. Miscellaneous Ruby and Testing 1 / 48

The Interpreter + Calling Functions. Scheme was designed by people. The Interpreter + Calling Functions. Clickers. Parentheses Matter We asked scheme

Regular expressions and regexp-based string operations

Regular Expression Reference

Transcription:

MTAT.03.295 Agile Software Development in the Cloud Lecture 4: More on Ruby Luciano García-Bañuelos University of Tartu

Regular Expressions Widely use tool for specifying (and implemen5ng) pa7ern matching processors and even lexical analyzers RegExp are first- class ci5zens in Ruby expr1 = Regexp.new('\(\d+\)') expr2 = /\(\d+\)/ Ruby s syntax is mostly borrowed from Perl s

A toy example =~ nil (cf. false) when no match index of the first occur. on the string, otherwise match nil when no match Instance of MatchData, otherwise def code_matcher(expr, str) if expr =~ str puts "Country code #{expr.match(str)} else puts 'No country code provided end end phone1 = '(372) 58 442-456' phone2 = '58 442-456' expr = /\(\d+\)/ code_matcher(expr, phone1) code_matcher(expr, phone2)

Character classes Example /[Rr]uby/ /Rub(y en)/ /[aeiou]/ /[0-9]/ /[a- za- Z]/ /[^aeiou]/ /[^0-9]/ Descrip2on Matches Ruby or ruby Matches Ruby or Ruben Matches a single lowercase vowel Matches a single digit Matches a single alphabe5c character Matches anything other than a lowercase vowel Matches anything other than a digit

Character classes (continuation) Abrevia2on Descrip2on \d A digit character, as [0-9] \D Any character except a digit, as [^0-9] \s Any whitespace, as [ \t\r\n\f] \S Any character except a whitespace \w Any word character, as [a- za- Z0-9_] \W Any character except a work character, as [^a- za- Z0-9_]. Any character except a whitespace POSIX character classes [:alpha:], [:alnum:], [:blank:], [:cntrl:], [:digit:], [:graph:], [:lower:], [:print:], [:punct:], [:space:], [:upper:], [:xdigit:]

Repetition and anchoring Example /ruby?/ /ruby*/ /ruby+/ /\d{3}/ /\d{3,}/ /\d{3,5}/ Descrip2on Matches rub or ruby Matches rub followed by 0 or more y s Matches rub followed by 1 or more y s Matches exactly three digits Matches 3 o more digits Matches 3, 4 or 5 digits Example /^Ruby/ /Ruby$/ /\ARuby/ /Ruby\Z/ /\bruby\b/ Descrip2on Matches rub or ruby Matches rub followed by 0 or more y s Matches rub followed by 1 or more y s Matches exactly three digits Matches Ruby if and only if found as single word

Matching groups exp1 = /(\d+)\d+(\d+)/ exp2 = /\d+/ m = exp1.match("_ 123 abc 345 x 67 end") puts "#{$1}, #{$2} m.captures exp2.match("_ 123 abc 345 x 67 end") puts "#{$1}, #{$2}" m = "_ 123 abc 345 x 67 end".scan(/(\d+)/) m.each { x puts x }

Matching and replacing str1 = "he said 'hello' to me" str2 = str1.dup puts str1.gsub(/he/,'he') puts str2.gsub(/\bhe\b/,'he')

Blocks class Array def iterate self.each_with_index do n, i self[i] = yield(n) end end end array = [1, 2, 3, 4] array.iterate do n n ** 2 end puts array.inspect

Blocks => Procs class Array def iterate(&code) self.each_with_index do n, i self[i] = code.call(n) end end end array = [1, 2, 3, 4] array.iterate do n n ** 2 end puts array.inspect

Procs can be reused class Array def iterate(code) self.each_with_index do n, i self[i] = code.call(n) end end end array_1 = [1, 2, 3, 4] array_2 = [2, 3, 4, 5] square = Proc.new { n n ** 2} array_1.iterate(square) array_2.iterate(square) puts array_1.inspect puts array_2.inspect

Lambdas class Array def iterate(code) self.each_with_index do n, i self[i] = code.call(n) end end end array = [1, 2, 3, 4] array.iterate( lambda { n n ** 2 }) puts array.inspect

Lambdas strictly check number of parameters def args(code) one, two = 1, 2 code.call(one, two) end args(proc.new{ a, b, c puts "Give me a #{a} and a #{b} and a #{c.class}"}) args(lambda{ a, b, c puts "Give me a #{a} and a #{b} and a #{c.class}"})

Lambdas perform diminutive returns def proc_return Proc.new { return "Proc.new"}.call return "proc_return method finished" end def lambda_return lambda { return "lambda" }.call return "lambda_return method finished" end puts proc_return puts lambda_return

Classes and Mixins module Persistence def load sfilename puts Reading #{sfilename} contents into 'my_data'" end def save sfilename puts "Persisting '#{@my_data}' into '#{sfilename}'" end end class MyClass include Persistence attr :my_data def data=(somedata) @my_data = somedata end end b = MyClass.new b.data = "This_15_my_pwd" b.save("passwords") b.load("passwords")

Finally metaprogramming class Polyglot @@greetings = {estonian: "Tere hommikust", english: "Good morning"} def Polyglot.add_language(language, greeting) @@greetings[language] = greeting if language.is_a? Symbol end def method_missing(name) unless @@greetings[name].nil? puts @@greetings[name] else super end end end a = Polyglot.new a.estonian a.english