Arrays Flashcards
array
an ordered list of elements that can be of any type.
First method
This finds the first element of an array. Array.first
Last method
This finds the last element of an array. Array.last
How do you reference any part of an array by its index number?
Array[3]
Pop method
Takes the last item off an array permanently
Push method
Add an item back to array permanently. Array.push or «_space;aka the shovel operator
Map method
Iterates over an array applying a block to each element of an array and returns a new array with those results. Array.map
Delete_at method
Eliminates the value at the index of the number passed to the method. Modifies array destructively. Array.delete_at
Delete method
Sometimes I will know the value I want to delete but not the index. This method permanently deletes all instances of this value from the array.
Uniq method
This method iterates through an array, deletes any duplicates that exist, then returns the result as a new array. Array.uniq
Uniq!
Destructive method of uniq
Select method
This method iterates over an array and returns a new array that includes any items that return true to the expression provided. numbers.select { |number| number > 4 }
It does not mutate the caller.
unshift method
Method that adds values to the start of the list in an array.
to_s method
Used to create a string representation of an array. Ruby does this behind the scenes when you use string interpolation on an array.
irb :001 > a = [1, 2, 3]
=> [1, 2, 3]
irb :002 > “It’s as easy as #{a}”
=> “It’s as easy as [1, 2, 3]”
include? method
Checks to see if the argument given is included in the array. It has a question mark in it which usually means you can expect it to return a Boolean true or false.
irb :001 > a = [1, 2, 3, 4, 5] => [1, 2, 3, 4, 5] irb :002 > a.include?(3) => true irb :003 > a.include?(6) => false
flatten method
This method can be used to take an array that contains nested arrays and create a one-dimensional array. Only destructive if it includes !
irb: 001 > a = [1, 2, [3, 4, 5], [6, 7]]
=> [1, 2, [3, 4, 5], [6, 7]]
irb: 002 > a.flatten
=> [1, 2, 3, 4, 5, 6, 7]
each_index method
Iterates through the array much like the each method. However, the variable represents the index number as opposed to the value at each index. It passes the index of the element into the block and you may do as you please with it.
irb: 001 > a = [1, 2, 3, 4, 5] => [1, 2, 3, 4, 5] irb: 002 > a.each_index { |i| puts "This is index #{i}" } This is index 0 This is index 1 This is index 2 This is index 3 This is index 4 => [1, 2, 3, 4, 5]
each _with_index method
Gives us the ability to manipulate both the value and the index by passing in two parameters into the block of code. The first is the value and the second is the index.
irb: 001 > a = [1, 2, 3, 4, 5] => [1, 2, 3, 4, 5] irb: 002 > a.each_with_index { |val, idx| puts "#{idx+1}. #{val}" } 1. 1 2. 2 3. 3 4. 4 5. 5 => [1, 2, 3, 4, 5]
sort method
This sorts an array by integer value.
Only destructive when followed by !
irb :001 > a = [5, 3, 8, 2, 4, 1]
=> [5, 3, 8, 2, 4, 1]
irb :002 > a.sort
=> [1, 2, 3, 4, 5, 8]
product method
Returns an array that is a combination of all the elements from all arrays.
irb :001 > [1, 2, 3].product([4, 5])
=> [[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]]