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