Ruby Intro - Enumerables Flashcards
What 2 methods create new arrays?
map or collect do
What’s the key difference between Map and Each?
Neither change the original array, however, map creates a new array while each doesn’t change anything but rather just runs through the enumerator. Map is used for it’s new array created. Each is used for the side effects of the enumeration process.
What 2 methods creates new arrays upon True result?
Select or Find_all do
nums.select do |i|
i % 3 == 0
What method tallies up total elements in array or hash?
count (for hashes, it counts total pairs)
num.count
What method returns true if an element exists in a collection?
include?(parameter)
What 3 methods boolean test for any element true/false in a given collection?
any? do
all? do
none? do
nums.any? do |i|
i % 2 == 0
example of Super elegant way to create an array with a method being done on each element?
(-10..-1).map(&:abs)
How do you use each_with_index? example
my_favorite_number = 42 numbers = [42, 3, 42, 5]
favorite_indices = [] numbers.each_with_index do |number, index| if number == my_favorite_number favorite_indices << index end end
What is the rule when using “i” as iterator/index in a loop?
i should start == 0, not 1.
the times do function starts at what number?
if 5.times do, it will iterate 0, 1, 2, 3, 4