Ruby Intro - Enumerables Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What 2 methods create new arrays?

A

map or collect do

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What’s the key difference between Map and Each?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What 2 methods creates new arrays upon True result?

A

Select or Find_all do

nums.select do |i|
i % 3 == 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What method tallies up total elements in array or hash?

A

count (for hashes, it counts total pairs)

num.count

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What method returns true if an element exists in a collection?

A

include?(parameter)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What 3 methods boolean test for any element true/false in a given collection?

A

any? do
all? do
none? do

nums.any? do |i|
i % 2 == 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

example of Super elegant way to create an array with a method being done on each element?

A

(-10..-1).map(&:abs)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you use each_with_index? example

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the rule when using “i” as iterator/index in a loop?

A

i should start == 0, not 1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

the times do function starts at what number?

A

if 5.times do, it will iterate 0, 1, 2, 3, 4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly