w1d1 Flashcards
creating arrays
empty_array = Array.new empty_array = []
accessing item by index (array)
some_fruits = [“pear”, “apple”]
some_fruits[0]
some_fruits[-1] from end
array ‘range’ example
some_fruits[0..1], some_fruits[0…2]
.. => start to finish
… => start to, but not including finish
accessing elements in order
array.each do |thing|
you should never modify array during iteration
true
setting and adding elements (array)
array[1] = “something else”
array «_space;“something”
array.push “something”
array + array
creates a new array that hold both values
add two arrays without ‘+’
array.concat(array_other)
array size example
array.count
check if array is empty
array.empty?
push pop array
array «_space;“something”
array.pop
array as queue example
array «_space;elements
array.shift
(first in, first out)
push element in front of array
array.unshift
interpolation
execute code in #{} inside string
finding elements in array
array.include?(something)