SWEN-250 Personal 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.
Language of the Month If it’s December, it must be Ruby! Adam Coffman and Brent Beer.
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
17-Jun-15 Assorted Ruby Details. The command line irb starts an interactive Ruby interpreter ruby starts Ruby, with input from the command line End with.
Guide To UNIX Using Linux Third Edition
Scripting with Ruby What is a scripting language? What is Ruby?
String Escape Sequences
Python Control of Flow.
SCRIPTING IN RUBY By Amber Bennett “Ruby is simple in appearance, but is very complex inside, just like our human body.” --Yukihiro Matsumoto.
1 An Introduction to Perl Part 1 CSC8304 – Computing Environments for Bioinformatics - Lecture 7.
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
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.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Regular Expressions in Perl CS/BIO 271 – Introduction to Bioinformatics.
GREP. Whats Grep? Grep is a popular unix program that supports a special programming language for doing regular expressions The grammar in use for software.
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
Scripting with Ruby What is a scripting language? What is Ruby?
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
RUBY by Ryan Chase.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
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.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
CMSC330 More Ruby. Last lecture Scripting languages Ruby language –Implicit variable declarations –Many control statements –Classes & objects –Strings.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
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:
Definition of the Programming Language CPRL
CSC 4630 Meeting 7 February 7, 2007.
Topic Pre-processor cout To output a message.
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)
CS 371 Web Application Programming
Introduction to Python
Control Flow Constructs: Conditional Logic
CSC 458– Predictive Analytics I, Fall 2017, Intro. To Python
Pamela Moore & Zenia Bahorski
University of Central Florida COP 3330 Object Oriented Programming
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Chapter 8: Control Structures
TCL/TK Tool Command Language/Tool Kit.
Introduction to Python
Chapter 14 Introduction to Ruby.
Python Primer 2: Functions and Control Flow
Introduction to Python
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
CS190/295 Programming in Python for Life Sciences: Lecture 6
CIT 383: Administrative Scripting
Ruby Testing 2, 11/9/2004.
Introduction to Primitive Data types
CSC 458– Predictive Analytics I, Fall 2018, Intro. To Python
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
CISC124 Labs start this week in JEFF 155. Fall 2018
Python Primer 1: Types and Operators
(more) Python.
CISC101 Reminders All assignments are now posted.
String methods 26-Apr-19.
Loops CGS3416 Spring 2019 Lecture 7.
Python Simple file reading
Introduction to Primitive Data types
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

SWEN-250 Personal Software Engineering Introduction to Ruby SWEN-250 Personal Software Engineering

A Bit of History Yukihiro "Matz'' Matsumoto Lineage Created a language he liked to work in. Been around since mid-90s. Caught on in early to mid 00s. Lineage Smalltalk – dynamic, OO-centric CLU – yield to blocks Pascal – basic concrete syntax AWK / Python / Perl – scripting & regular expressions Matz's own predilections

Ruby Characteristics Everything is an object – everything. 3.times { puts "hello" } "Mike is smart".sub(/Mike/, "Pete") str = str[0..9] unless str.length < 10 Every statement is an expression: Generally the last value computed. No need for return – but it's there anyway. Rich built in data types: String Array Hash RegExp Range Unbounded numbers (factorial) Blocks & procs Anonymous functions

Exploring Ruby ri – Ruby information irb – Interactive Ruby Script files: filename.rb (where you write the code) Running ruby: ruby filename.rb NOTE: No separate compile/ run (vs. C)

Ruby Control Structures: Selection if condition statements elsif condition else end Conditions in Ruby Comparisons, etc., return a boolean: true (the only member of TrueClass) false (the only member of FalseClass) Evaluating conditions false evaluates to false. nil evaluates to false. Everything else is true (including 0). unless condition statements end Statement Modifiers (a la Perl) statement if condition statement unless condition

Ruby Control Structures: Loops while condition statements end until condition statements end begin statements end while condition end until condition We don't need no stinkin' loops! Early Termination next break redo … unless we do …

But, For Completeness loop for statement loop { puts "forever" } loop do line = gets break if ! line puts line end for statement collection.each do | v | statements end for v in collection statements end

Iterators Explict loops are rare in Ruby Instead, we usually use iterators Iterators are defined on collection classes "Push" elements into a block one at a time. The basic iterator is each. Show with arrays (the simplest collection) fibo = [ 1, 2, 3, 5, 8 ] fibo.each { | value | puts "The next value is #{value }" } fibo.each_index { | i | puts "fibo[#{i}] = #{fibo[i]}" } fibo.select { | value | value % 2 == 1 } fibo.inject(0) { | sum, value | sum += value } puts "Total = #{fibo.inject(0) { | s, v | s += v }}" select reject collect detect inject

Strings Literals Operators Some of the methods (many have ! variants) "abcdef" vs. 'abcdef' %q{xyz#{1}}  non-interpolated String "abc #{3 % 2 == 1} def" %Q{xyz#{1}}  interpolated String Operators + and += s1 = "a" + "b" ; s1 += "c" * "oops! " * 3 [] should be obvious, but "abcd"[1..2] == < <=> comparisons =~ and !~ r.e. match (and not match) Some of the methods (many have ! variants) capitalize sub(r.e, str) downcase include?(str) upcase index(str or r.e.) puts "Betty's pie shop" puts 'Betty\'s pie shop' Because "Betty's" contains an apostrophe, which is the same character as the single quote, in the second line we need to use a backslash to escape the apostrophe so that Ruby understands that the apostrophe is in the string literal instead of marking the end of the string literal. The backslash followed by the single quote is called an escape sequence. Single quotes Single quotes only support two escape sequences. \' – single quote \\ – single backslash Except for these two escape sequences, everything else between single quotes is treated literally. Double quotes Double quotes allow for many more escape sequences than single quotes. They also allow you to embed variables or Ruby code inside of a string literal – this is commonly referred to as interpolation. puts "Enter name" name = gets.chomp puts "Your name is #{name}" Escape sequences Below are some of the more common escape sequences that can appear inside of double quotes. \" – double quote \a – bell/alert \b – backspace \r – carriage return \n – newline \s – space \t – tab

And of course … Strings

Single vs Double quotes puts "Betty's pie shop" puts 'Betty\'s pie shop' Because "Betty's" contains an apostrophe, which is the same character as the single quote, in the second line we need to use a backslash to escape the apostrophe so that Ruby understands that the apostrophe is in the string literal instead of marking the end of the string literal. The backslash followed by the single quote is called an escape sequence.

Single vs Double quotes Single quotes Single quotes only support two escape sequences. \' – single quote \\ – single backslash Except for these two escape sequences, everything else between single quotes is treated literally. NOT JUST FOR CHAR!

Single vs Double quotes Double quotes allow for many more escape sequences than single quotes. They also allow you to embed variables or Ruby code inside of a string literal – this is commonly referred to as interpolation. puts "Enter name" name = gets.chomp puts "Your name is #{name}" ‘name’ will be added/ inserted to the string Alternative to using ‘+’

Escape sequences Below are some of the more common escape sequences that can appear inside of double quotes. \" – double quote \\ – single backslash \a – bell/alert \b – backspace \r – carriage return \n – newline \s – space \t – tab

Further examples %q(no interpolation) %Q(interpolation and backslashes) %(interpolation and backslashes) Example $ str = ‘mystring' $ %q[#{str} “anotherstring"] => "\#{str} \“anotherstring\"“ $ %Q[#{str} “anotherstring"] => “mystring \“anotherstring\"“ $ %[#{str} “anotherstring"] => “mystring \“anotherstring\"" %(…) is used to create a string object

Arrays Literals Operators Some of the methods a = [ 1, "foo", [ 6, 7, 8 ], 9.87 ] b = %w{ now is the time for all good men }  Interpolated array of words Operators & (intersection) + (catenation) - (difference) * int (repetition) * str (join w/str as separator) [] []= as expected for simple indices << obj (push on end) Some of the methods [1, "hello", 3].collect { |v| v * 2 } # alias map [1, 2, 5].include?(2) [1, 2, 5].first [1, 2, 5].last [1, 2, 5].length [1, 2, 5].empty?

Hashes Literals Operators Some methods { "door" => "puerta", "pencil" => " lapiz" } new Hash( default ) Operators h[key] h[key] = value Some methods each each_key each_value empty? has_key? has_value? size keys (returns array) values (returns array) sort (returns an array of 2-element arrays) sort { |p1, p2| expression returning -1, 0, +1 }

I/O Class File Class Dir f = File.new(name, mode) f.close name is a string giving the file name (host dependent). mode is an access string: "r", "rw", "w", "w+" f.close f.puts, f.printf, f.gets, etc. puts, printf are implicitly prefixed by $stdout. gets is implicitly prefixed by $stdin File.open(name, mode) block – open the file name, call block with the open file, close file when block exits. Class Dir d = Dir.new(name) – open named directory. d.close Dir.foreach(name) block – pass each file name to block.

RegExps Literals /regular expression/ %r@regular expression@ delimiter is @ /regular expression/i case insensitive Resource https://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm Rubular http://rubular.com/

RegExps Examples 'Some cats here'.gsub(/cats/,'dogs') 'xxAAyyBBzz'.gsub(/A+[^B]*B+/,'\&<->\&') 'xxAAyyBBzz'.gsub(/(A+)([^B]*)(B+)/,'\3\2\1') 'xx(AA)Azz'.gsub(/\(A+\)/,'###')

Miscellaneous (1) Functions Requiring modules call: puts "abc" or puts("abc") define: def putNtimes(string, count) puts string * count end Requiring modules require string Looks for string.rb and imports whatever is in there. Typically service functions, classes, etc. Looks in "standard" locations as well as current directory. Example: require 'pp' Makes a function pp available. Similar to puts, but presents structures in a nested, easier to read format.

Miscellaneous Symbols Duck typing: "If it looks like a duck . . ." :foobar, :myname like a string but unique, immutable, and fast Often used as hash keys, identifiers, etc. Duck typing: "If it looks like a duck . . ." def putlengths anArray anArray.each { |x| puts x.length } end putlengths [ [1, 2, 3], "abcde", {"a" => "b", "c" => "d"} ] In Ruby, we rely less on the type (or class) of an object and more on its capabilities. Hence, Duck Typing means an object type is defined by what it can do, not by what it is. Duck Typing refers to the tendency of Ruby to be less concerned with the class of an object and more concerned with what methods can be called on it and what operations can be performed on it. In Ruby, we would use respond_to? or might simply pass an object to a method and know that an exception will be raised if it is used inappropriately.

ON TO THE ACTIVITY