Ruby Arrays Flashcards
array
An array is an ordered list of elements that can be of any type.
irb :001 > [1, ‘Bob’, 4.33, ‘another string’]
irb :002 > array = [1, ‘Bob’, 4.33, ‘another string’]
pop method
If you’d like to take the last item off of an array permanently, you can use the pop method.
irb :007 > array.pop
=> “another string”
irb :008 > array
=> [1, “Bob”, 4.33]
push method
If you’d like to add that item back to the array permanently, you can use the push method and send it the parameters you’d like to add.
irb :009 > array.push(“another string”)
=> [1, “Bob”, 4.33, “another string”]
shovel operator (<
irb :010 > array.pop
=> “another string”
irb :011 > array «_space;“another string”
=> [1, “Bob”, 4.33, “another string”]
map method
The map method iterates over an array applying a block to each element of the array and returns a new array with those results.
irb :001 > a = [1, 2, 3, 4]
=> [1, 2, 3, 4]
irb :002 > a.map { |num| num**2 }
=> [1, 4, 9, 16]
map creates and returns a new array containing the values returned by the block
collect method
The collect method is an alias to map - they do the same thing.
irb :003 > a.collect { |num| num**2 }
=> [1, 4, 9, 16]
irb :004 > a
=> [1, 2, 3, 4]
delete_at method
eliminate the value at a certain index from your array. Once you call this method, you are changing your array permanently.
irb :005 > a.delete_at(1)
=> 2
irb :006 > a
=> [1, 3, 4]
delete method
sometimes you will know the value that you want to delete, but not the index.
The delete method permanently deletes all instances of the provided value from the array.
irb :007 > my_pets = [“cat”, “dog”, “bird”, “cat”, “snake”]
=> [“cat”, “dog”, “bird”, “cat”, “snake”]
irb :008 > my_pets.delete(“cat”)
=> “cat”
irb :009 > my_pets
=> [“dog”, “bird”, “snake”]
uniq method
his iterates through an array, deletes any duplicate values that exist, then returns the result as a new array.
Once again, notice that the uniq method did not modify the original b array; it returned a new array with the duplicates removed.
irb :010 > b = [1, 1, 2, 2, 3, 3, 4, 4] => [1, 1, 2, 2, 3, 3, 4, 4] irb :011 > b.uniq => [1, 2, 3, 4] irb :012 > b => [1, 1, 2, 2, 3, 3, 4, 4]
uniq!
If you add the bang operator (!) to this method, you can perform the uniq function destructively. Much like the way the delete method works.
uniq and uniq! are two different methods for Ruby Arrays. You cannot simply append a ! onto any method and achieve a destructive operation.
irb :013 > b = [1, 1, 2, 2, 3, 3, 4, 4] => [1, 1, 2, 2, 3, 3, 4, 4] irb :014 > b.uniq! => [1, 2, 3, 4] irb :015 > b => [1, 2, 3, 4]
select method
This method iterates over an array and returns a new array that includes any items that return true to the expression provided.
irb :001 > numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
irb :002 > numbers.select { |number| number > 4 }
=> [5, 6, 7, 8, 9, 10]
irb :003 > numbers
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The select method selects all of the numbers that are greater than 4 and returns them in an array. It does not mutate the caller (the original numbers array is unmodified).
bang operator (!)
The bang operator (!) at the end of the method name usually indicates that the method will change (or mutate) the caller permanently. Unfortunately this is not always the case. It is a good rule to be wary of any method that has the bang operator and to make sure to check the Ruby documentation to see if it will behave destructively (the word “destructive” here just means mutating the caller).
unshift method
The unshift method adds the arguments that you specify to the front of the list.
irb :001 > a = [1, 2, 3] => [1, 2, 3] irb :002 > b = [2, 3, 4] => [2, 3, 4] irb :003 > a == b => false irb :004 > b.pop => 4 irb :005 > b.unshift(1) => [1, 2, 3] irb :006 > a == b => true
include? method
The include? method checks to see if the argument given is included in the array.
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
The flatten method can be used to take an array that contains nested arrays and create a one-dimensional array.
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]