Arrays Flashcards
build up multi-dimensional arrays
empty_table = Array.new(3) { Array.new(3) } #=> [[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]
return the first n elements of an array
arr.take(3) #=> [1, 2, 3]
special methods first
arr.first #=> 1
special methods last
arr.last #=> 6
returning the elements after n elements
arr.drop(3) #=> [4, 5, 6]
arr = [1, 2, 3, 4, 5, 6]
arr[2]
=> 3
arr = [1, 2, 3, 4, 5, 6]
arr[100]
=> nil
arr = [1, 2, 3, 4, 5, 6]
arr[-3]
=> 4
arr = [1, 2, 3, 4, 5, 6]
arr[2, 3]
=> [3, 4, 5]
arr = [1, 2, 3, 4, 5, 6]
arr[1..4]
=> [2, 3, 4, 5]
arr = [1, 2, 3, 4, 5, 6]
arr.at(0)
=> 1
arr = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]
arr.fetch(100)
=> IndexError: index 100 outside of array bounds: -6…6
arr = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]
arr.fetch(100, “oops”)
=> “oops”
arr = [1, 2, 3, 4, 5, 6]
arr.first
=> 1
arr = [1, 2, 3, 4, 5, 6]
arr.last
=> 6
browsers = [‘Chrome’, ‘Firefox’, ‘Safari’, ‘Opera’, ‘IE’]
browsers.length #=> 5
=> 5
browsers = [‘Chrome’, ‘Firefox’, ‘Safari’, ‘Opera’, ‘IE’]
browsers.count
=> 5
browsers = [‘Chrome’, ‘Firefox’, ‘Safari’, ‘Opera’, ‘IE’]
browsers.empty?
=> false
browsers = [‘Chrome’, ‘Firefox’, ‘Safari’, ‘Opera’, ‘IE’]
browsers.include?(‘Konqueror’)
=> false
arr = [1, 2, 3, 4]
arr.push(5)
=> [1, 2, 3, 4, 5]
arr = [1, 2, 3, 4]
arr
=> [1, 2, 3, 4, 6]
add item to array
use arr.push(5) or arr
add a new item to the beginning of an array
arr.unshift(0) #=> [0, 1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
arr.unshift(0)
=> [0, 1, 2, 3, 4, 5, 6]
add a new element to an array at any position
arr.insert(3, ‘apple’) #=> [0, 1, 2, ‘apple’, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
arr.insert(3, ‘apple’)
=> [0, 1, 2, ‘apple’, 3, 4, 5, 6]
insert multiple values at once
arr.insert(3, 'orange', 'pear', 'grapefruit') #=> [0, 1, 2, "orange", "pear", "grapefruit", "apple", 3, 4, 5, 6]
[0, 1, 2, ‘apple’, 3, 4, 5, 6]
arr.insert(3, ‘orange’, ‘pear’, ‘grapefruit’)
=> [0, 1, 2, “orange”, “pear”, “grapefruit”, “apple”, 3, 4, 5, 6]
removes the last element in an array and returns it
arr = [1, 2, 3, 4, 5, 6]
arr.pop #=> 6
arr #=> [1, 2, 3, 4, 5]
arr = [1, 2, 3, 4, 5, 6]
arr.pop
arr #=> [1, 2, 3, 4, 5]
retrieve and at the same time remove the first item
arr.shift #=> 1
arr #=> [2, 3, 4, 5]
arr #=> [1, 2, 3, 4, 5]
arr.shift
#=> 1 arr #=> [2, 3, 4, 5]
delete an element at a particular index
arr.delete_at(2) #=> 4
arr #=> [2, 3, 5]
arr #=> [2, 3, 4, 5]
arr.delete_at(2) #=> 4
arr #=> [2, 3, 5]
delete a particular element anywhere in an array
arr = [1, 2, 2, 3]
arr.delete(2) #=> [1, 3]
arr = [1, 2, 2, 3]
arr.delete(2)
=> [1, 3]
arr = [1, 2, 2, 3]
arr.delete(2)
delete a particular element anywhere in an array
arr.delete(2) #=> [1, 3]
remove nil values from an array
arr = [‘foo’, 0, nil, ‘bar’, 7, ‘baz’, nil]
arr.compact #=> [‘foo’, 0, ‘bar’, 7, ‘baz’]
arr #=> [‘foo’, 0, nil, ‘bar’, 7, ‘baz’, nil]
arr.compact! #=> [‘foo’, 0, ‘bar’, 7, ‘baz’]
arr #=> [‘foo’, 0, ‘bar’, 7, ‘baz’]
arr = [‘foo’, 0, nil, ‘bar’, 7, ‘baz’, nil]
arr.compact
=> [‘foo’, 0, ‘bar’, 7, ‘baz’]
arr #=> [‘foo’, 0, nil, ‘bar’, 7, ‘baz’, nil]
arr.compact!
arr #=> [‘foo’, 0, ‘bar’, 7, ‘baz’]
remove duplicate elements from an array, non-destructive
arr = [2, 5, 6, 556, 6, 6, 8, 9, 0, 123, 556]
arr.uniq #=> [2, 5, 6, 556, 8, 9, 0, 123]
arr = [2, 5, 6, 556, 6, 6, 8, 9, 0, 123, 556]
arr = [2, 5, 6, 556, 6, 6, 8, 9, 0, 123, 556]
arr.uniq
[2, 5, 6, 556, 8, 9, 0, 123]
non-destructive arr = [2, 5, 6, 556, 6, 6, 8, 9, 0, 123, 556]
arr = [2, 5, 6, 556, 6, 6, 8, 9, 0, 123, 556]
arr.uniq!
[2, 5, 6, 556, 8, 9, 0, 123]
destructive arr = [2, 5, 6, 556, 8, 9, 0, 123]
remove duplicate elements from an array, destructive
arr = [2, 5, 6, 556, 6, 6, 8, 9, 0, 123, 556]
arr.uniq! #=> [2, 5, 6, 556, 8, 9, 0, 123]
arr = [2, 5, 6, 556, 8, 9, 0, 123]
start at iterating over arrays, http://docs.ruby-lang.org/en/2.0.0/Array.html
start at iterating over arrays,http://docs.ruby-lang.org/en/2.0.0/Array.html