Download presentation
Presentation is loading. Please wait.
1
CIT 383: Administrative Scripting
Arrays CIT 383: Administrative Scripting
2
CIT 383: Administrative Scripting
Topics Creating Arrays Indexing Arrays Array Methods Iterators CIT 383: Administrative Scripting
3
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
4
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
5
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
6
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
7
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
8
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
9
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
10
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
11
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
12
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
13
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
14
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
15
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
16
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
17
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
18
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
19
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.