Arrays Flashcards
What does “each” method return on an array?
It always returns the original array. e.g. x = [1, 2, 3, 4, 5] x.each do |a| a + 1 end
=>[1, 2, 3, 4, 5]
What do “array.first” and “array.last” do? (+examples)
They return the first and the last element of the array.
e.g.
array = [1,2,3,4,5]
array.first
=>1
array.last
=>5
What are the two result
options after a method is evaluated?
It either returns new data or modifies the original data.
new data:(does not mutate the caller)
.sort (arranges numbers in the array in order)
modifies: (mutate the caller)
.pop (takes the last element of the array)
How do you add an argument to the beginning and take one away from the end of a(n) array/list?
a = [3,4,5,6]
a.unshift(10)
=> [10,3,4,5,6]
a.pop
=>[10,3,4,5]
How to add an item back to the array permanently?
a = [2,5,6]
a.push(3)
=>[2,5,6,3]
What is the alternative to the .pop method?
«
a = [2,3,4,6]
a «_space;6
=>[2,3,4]
What is the difference between .map and .collect methods and how do they work?
They are the same.
a = [1,2,3,4]
a.collect {|num| num*5}
=> [5,10,15,20]
a.map {|num| num*5}
=> [5,10,15,20]
What is the difference between the “delete” and “delete_at” methods?
Both can delete an element
from the array, the “delete_at” targets it by its index (array[2]) while the “delete” targets it by its value (array[“lemon”]).
Both mutate the caller.
How can you iterate through an array and delete the duplicated values?
With the .uniq method. e.g. array = [1,2,3,4,2,1,3,2,1] array.uniq => [1,2,3,4]
It doesn’t mutate the caller.
Write a method that iterates through an array and return those numbers that are less than 8!
array = [2,6,9,8,12,1,4,7,25]
array.select {|num| num < 8}
What is the alternative to the .pop method?
«
a = [2,3,4,6]
a «_space;6
=>[2,3,4]
What is the difference between .map and .collect methods and how do they work?
They are the same.
a = [1,2,3,4]
a.collect {|num| num*5}
=> [5,10,15,20]
a.map {|num| num*5}
=> [5,10,15,20]
What is the difference between the “delete” and “delete_at” methods?
Both can delete an element
from the array, the “delete_at” targets it by its index (array[2]) while the “delete” targets it by its value (array[“lemon”]).
Both mutate the caller.
How can you iterate through an array and delete the duplicated values?
With the .uniq method. e.g. array = [1,2,3,4,2,1,3,2,1] array.uniq => [1,2,3,4]
It doesn’t mutate the caller.
Write a method that iterates through an array and return those numbers that are less than 8!
array = [2,6,9,8,12,1,4,7,25]
array.select {|num| num < 8}
=> [2,6,1,4,7]
What does the bang suffix (!) indicate usually?
It indicates that the method will change (or mutate) the caller permanently.
How do you compare arrays?
With the equality operator (==)
The result is a boolean value.
e.g.
a = [1,2,3]
b = [2,5,6]
=> false
What do you use “include?” method for?
It checks to see if the argument given is included in the array.
It returns a boolean value.
e.g.
a = [2,6,8,9]
a.include?(8)
=> true
How do you turn an array containing nested arrays into a one-dimensional array?
Use the ".flatten" method! e.g. a = [1,2, [3,4,5], [2,9] a.flatten =>[1,2,3,4,5,2,9]
It doesn’t mutate the caller.
What is the difference between “.each”, “.each_index” and “.each_with_index”?
They all iterate through an array.
“.each” represents the value at each index
“.each_index” represents the index number
“.each_with_index” can manipulate both the value and the index by passing two parameters to the block of code.
e.g. a = ["a", "b", "c"] a.each_with_index {|val, idx| puts "#{idx+1}. #{val}" } 1. a 2. b 3. c
What does the .sort array do?
It is a handy way to order an array.
a = [2,9,3,5,4,6]
a.sort
=> [2,3,4,5,6,9]
It mutates the caller if we add (!).
It works with strings and floats as well.
Which method combines two arrays and returns their combination of all elements as an array?
The “.product” method.
e.g.
[“a”, “d”].product([1,3,8])
=> [[“a”, 1], [“a”, 3], [“a”, 8], [“b”, 1], [“b.”, 3], [“b”, 8]]
What is the difference between “.each” and “.map”?
Both iterate over a collection. ".each" it does the operation and returns the original value. ".map" creates and returns a new array containing the values by the block. e.g. a = [1,2,3] a.each {|e| print e} 1,2,3 => [1,2,3]
a = [1,2,3]
a.map {|e| e**2}
=>[1,4,9]
Insert 3 into the following array, after 1!
array = [1,2,3,6,9]
puts array.insert(1, 3)
=> [1,3,2,3,6,9]
(inserts before the element’s index, in this case we want it after 1 which is index 0, next element (3) is index 1 that’s why we use it)