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