Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIT 383: Administrative Scripting

Similar presentations


Presentation on theme: "CIT 383: Administrative Scripting"— Presentation transcript:

1 CIT 383: Administrative Scripting
Flow Control and Arrays CIT 383: Administrative Scripting

2 CIT 383: Administrative Scripting
Topics Boolean Expressions Conditionals While Loops Creating Arrays Indexing Arrays Array Methods Iterators CIT 383: Administrative Scripting

3 CIT 383: Administrative Scripting
What are true and false? false is false, the one member of the FalseClass nil, the one member of NilClass true is true, the one member of the TrueClass anything that’s not false, including 0, “”, NaN,... CIT 383: Administrative Scripting

4 CIT 383: Administrative Scripting
Comparison Operators Binary operators that return true or false. == equals != not equals > greater than >= greater than or equal to < less than <= less than or equal to CIT 383: Administrative Scripting

5 CIT 383: Administrative Scripting
Boolean Operators Operate on Boolean values. ! not not not (lower precedence) && and and and (lower precedence) || or or or (lower precedence) CIT 383: Administrative Scripting

6 CIT 383: Administrative Scripting
Boolean Expressions x == 0 y > 1 z <= 5 x == 0 && y > 1 x == 0 && y > 1 || z <= 5 is_x_zero = x == 0 big_y = y > 2**16 is_x_zero && big_y CIT 383: Administrative Scripting

7 CIT 383: Administrative Scripting
Conditionals if statement if statement with else clause elsif if expressions unless statement post-conditionals case statement CIT 383: Administrative Scripting

8 CIT 383: Administrative Scripting
if statement expr code expression true false if expression code end expr color = hilight x%2 == 0 true false if x % 2 == 0 color = hilight end CIT 383: Administrative Scripting

9 CIT 383: Administrative Scripting
Differences from Java Parentheses not required around expression expression begins after if and space expression ends with newline If statement must be terminated by an end There is no punctuation like Java’s braces. CIT 383: Administrative Scripting

10 CIT 383: Administrative Scripting
if statement with else if expression code1 else code2 end true false expression code1 code2 CIT 383: Administrative Scripting

11 Multiple conditionals with elsif
if expression1 code1 elsif expression2 code2 . elsif expressionN codeN else code end if x == 1 name = “one” elsif x == 2 name = “two” elsif x == 3 name = “three” elsif x == 4 name = “four” else name = “lots” end CIT 383: Administrative Scripting

12 CIT 383: Administrative Scripting
if expressions Ruby has no statements, only expressions. Everything returns a value, even if it’s nil. if returns the value of the last expression evaluated. Example: name = if x == 1 then “one” elsif x == 2 then “two” elsif x == 3 then “three” elsif x == 4 then “four” else “many” end CIT 383: Administrative Scripting

13 CIT 383: Administrative Scripting
unless statement unless expression code end true expression false code expr unless expression code1 else code2 end CIT 383: Administrative Scripting

14 CIT 383: Administrative Scripting
Post-conditionals Pre-conditional form if expression then code end Equivalent post-conditional form code if expression Examples puts message if debug > 1 puts message if !quiet && verbose CIT 383: Administrative Scripting

15 Equivalent if statement
Case statement Case statement Equivalent if statement name = case when x == 1 then “one” else “many” end name = if x == 1 then “one” elsif x == 2 then “two” elsif x == 3 then “three” elsif x == 4 then “four” else “many” end CIT 383: Administrative Scripting

16 Alternate case statement format
name = case x when 1 then “one” when 2 then “two” when 3 then “three” when 4 then “four” else “many” end CIT 383: Administrative Scripting

17 CIT 383: Administrative Scripting
while loop while expression code end false expression true code expr x = 0 while x < 10 puts x x = x + 1 end expr puts x x = x + 1 x < 10 true false x = 0 CIT 383: Administrative Scripting

18 CIT 383: Administrative Scripting
until loop until expression code end true expression false code expr x = 0 until x > 10 puts x x = x + 1 end x = 0 true x > 10 false expr puts x x = x + 1 CIT 383: Administrative Scripting

19 CIT 383: Administrative Scripting
Arrays in Ruby a = [10, 20, 30, 40, 50, 60] a[0] 10 20 a[1] 30 a[2] a[3] 40 a[4] 50 a[5] 60 a[-1] CIT 383: Administrative Scripting

20 CIT 383: Administrative Scripting
Arrays of Strings Define the same way as arrays of integers a = [‘spam’, ‘and’, ‘eggs’, ‘for’, ‘breakfast’] Alternative syntax for strings w/o spaces: a = %w[spam and eggs for breakfast] a = %w|spam and eggs for breakfast| %W works like double quoted strings a = [“\t”, “\n”, “\r”] a = %W(\t \n \r) CIT 383: Administrative Scripting

21 CIT 383: Administrative Scripting
Creating Empty Arrays Use the [] notation a = [] Create like any other object a = Array.new Similar to str = “” str = String.new CIT 383: Administrative Scripting

22 Arrays of Different Types
Ruby arrays can have data of different types a=[1,2.718,‘a string’,“\t”,[3,4]] Use like any other array a[0] == 1 a[1] == 2.718 a[2] == ‘a string’ a[3] == “\t” a[4] == [3, 4] CIT 383: Administrative Scripting

23 CIT 383: Administrative Scripting
Indexing Arrays Single indexes a[n] is nth element from beginning starting at 0 a[-n] is nth element from end starting at -1 Double indexes (slicing) a[0,1] == a[0] a[0,2] == [ a[0], a[1] ] a[1,3] == [ a[1], a[2], a[3] ] a[-2, 2] == [ a[-2], a[-1] ] CIT 383: Administrative Scripting

24 CIT 383: Administrative Scripting
Index Assignments Single index a[n] = 5 sets nth element to be 5 If a[n] isn’t already defined, extends array to be long enough to have an nth element. Double index a[0,2] = [10,20] sets first two elements to 10,20 a[0,0] = [1,2,3] inserts 3 elements at beginning a[0,2] = [] deletes first two elements of array CIT 383: Administrative Scripting

25 CIT 383: Administrative Scripting
Array Operations Concatenation a = [1,2,3] + [4,5] # [1,2,3,4,5] a = a + [6,7,8] # [1,2,3,4,5,6,7,8] Subtraction [1,2,3,4,5,6] – [2,3,4] # [1,5,6] Append a = [] a << # [1] a << 2 << # [1, 2,3] a << [4,5,6] # [1,2,3,[4,5,6]] CIT 383: Administrative Scripting

26 CIT 383: Administrative Scripting
Array Operations Union: | Concatenates then removes duplicates. Intersection: & Array of elements in both arrays, no duplicates. Examples a = [1,1,2,2,3,3,4] b = [5,5,4,4,3,3,2] a | b # [1,2,3,4,5] b | a # [5,4,3,2,1] a & b # [2,3,4] b & a # [4,3,2] CIT 383: Administrative Scripting

27 CIT 383: Administrative Scripting
Array Methods reverse Reverses order of array elements. sort Sorts array elements from lowest to highest All array elements must be of same type. sort! Previous methods return altered array. Methods ending in ! change array in place. CIT 383: Administrative Scripting

28 Using Arrays w/o Indexing
We often need just a list of items to: Add items from the list. Remove items from the list. Perform an action for each item of the list. We don’t need to deal with indices since The 5th item isn’t special. We just want to add, remove, or do something for each item of the list. Leaves only one choice: add and remove from End of array (push and pop) Beginning of array (shift and unshift) CIT 383: Administrative Scripting

29 CIT 383: Administrative Scripting
Push and pop push adds an element to end of array. 10 a[0] a[0] 10 20 20 a.push(40) 30 30 a[-1] 40 a[-1] CIT 383: Administrative Scripting

30 CIT 383: Administrative Scripting
Push and pop pop removes an element from end of array. 10 a[0] a[0] 10 20 20 x = a.pop 30 a[-1] 30 a[-1] 40 x == 40 CIT 383: Administrative Scripting

31 CIT 383: Administrative Scripting
Shift and unshift unshift adds an element to start of array. 5 a[0] a[0] 10 10 20 a.unshift(5) 20 30 a[-1] 30 a[-1] CIT 383: Administrative Scripting

32 CIT 383: Administrative Scripting
Shift and unshift shift removes an element f/ start of array. 10 a[0] a[0] 5 20 10 x = a.shift 30 a[-1] 20 a[-1] 30 x == 5 CIT 383: Administrative Scripting

33 CIT 383: Administrative Scripting
The each iterator Each executes code for each item of array Assigns current element to |variable| Executes block of code. Sets current element to next element. Example: langs = %w(python ruby scheme) langs.each do |language| puts “#{language} is good.” end CIT 383: Administrative Scripting

34 CIT 383: Administrative Scripting
The each iterator array.each do |var| code end n = 0 var = array[n] code false is n last element? n = n + 1 true CIT 383: Administrative Scripting

35 CIT 383: Administrative Scripting
References Michael Fitzgerald, Learning Ruby, O’Reilly, 2008. David Flanagan and Yukihiro Matsumoto, The Ruby Programming Language, O’Reilly, 2008. Hal Fulton, The Ruby Way, 2nd edition, Addison-Wesley, 2007. Dave Thomas with Chad Fowler and Andy Hunt, Programming Ruby, 2nd edition, Pragmatic Programmers, 2005. CIT 383: Administrative Scripting


Download ppt "CIT 383: Administrative Scripting"

Similar presentations


Ads by Google