Common Enumerables Notes Day 1 Flashcards

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

.all?

A

Return true when all elements result in true when passed into the block.

p [2, 4, 6].all? { |el| el.even? } # => true
p [2, 3, 6].all? { |el| el.even? } # => false

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

.any?

A

Return true when all at least one element results in true when passed into the block.

p [3, 4, 7].any? { |el| el.even? } # => true
p [3, 5, 7].any? { |el| el.even? } # => false

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

.none?

A

Return true when no elements of result in true when passed into the block.

p [1, 3, 5].none? { |el| el.even? } # => true
p [1, 4, 5].none? { |el| el.even? } # => false

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

.one?

A

Return true when exactly one element results in true when passed into the block.

p [1, 4, 5].one? { |el| el.even? } # => true
p [1, 4, 6].one? { |el| el.even? } # => false
p [1, 3, 5].one? { |el| el.even? } # => false

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

.count

A

Return a number representing the count of elements that result in true when passed into the block.

p [1, 2, 3, 4, 5, 6].count { |el| el.even? } # => 3
p [1, 3, 5].count { |el| el.even? } # => 0

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

.sum

A

Return the total sum of all elements

p [1, -3, 5].sum # => 3

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

.max and .min

A

Return the maximum or minimum element

p [1, -3, 5].min # => -3
p [1, -3, 5].max # => 5
p [].max # => nil

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

.flatten or .flatten!

A

Return the 1 dimensional version of any multidimensional array

multi_d = [
    [["a", "b"], "c"],
    [["d"], ["e"]],
    "f"
]

p multi_d.flatten # => [“a”, “b”, “c”, “d”, “e”, “f”]

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