Enumerable Flashcards

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

Ruby Enumerable

returns true if the block never returns false or nil

A

all? { |obj| block } => true or false

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

Ruby Enumerable

returns true if the block ever returns a value other than false or nil

A

any? { |obj| block } => true or false

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

Ruby Enumerable

returns a new array with the results of running block once for every element in enum

A

map { |obj| block } => array

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

Ruby Enumerable

returns the number of elements for which the block yields a true value

A

count { |obj| block } => int

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

Ruby Enumerable

returns the first for which block is not false

A

find(ifnone = nil) { |obj| block } => obj or nil

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

Ruby Enumerable

alias for select

A

find_all { |obj| block } => array

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

Ruby Enumerable

groups the collection by result of the block; returns a hash where the keys are the evaluated result from the block and the values are arrays of elements in the collection that correspond to the key

A

group_by { |obj| block } => a_hash

ex

(1..6).group_by { |i| i%3 } #=> {0=>[3, 6], 1=>[1, 4], 2=>[2, 5]}

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

Ruby Enumerable

returns true if any member of enum equals obj

A

include?(obj) => true or false

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

Ruby Enumerable

combines all elements of enum by applying a binary operation, specified by a block or a symbol that names a method or operator

A

inject(initial, sym) => obj inject(initial) { |memo, obj| block } => obj

ex

(5..10).inject(0, &:+) => 45 (5..10).inject(0) { |memo, num| memo + num } => 45

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

Ruby Enumerable

returns the object in enum with the maximum value; can use a block to implement a \<=\> b

A

max(n) { |a,b| block } => obj

ex

a = %w(albatross dog horse)

a. max #=> “horse”
a. max { |a, b| a.length b.length } #=> “albatross”

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

Ruby Enumerable

alias of include?

A

member?(obj) => true or false

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

Ruby Enumerable

opposite of max

A

min(n) { |a,b| block }

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

Ruby Enumerable

returns true if the block never returns true for all elements

A

none? { |obj| block } => true or false

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

Ruby Enumerable

returns true if the block returns true exactly once

A

one? { |obj| block } => true or false

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

Ruby Enumerable

opposite of select

A

reject { |obj| block } => array

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

Ruby Enumerable

returns an array containing all elements of enum for which the given block returns a true value

A

select { |obj| block } => array

17
Q

Ruby Enumerable

returns an array containing the items in enum sorted, either according to their own method, or by using the results of the supplied block

A

sort { |a, b| block } => array

18
Q

Ruby Enumerable

takes one element from enum and merges corresponding elements from each args. This generates a sequence of n-element arrays, where n is one more than the count of arguments

A

zip(arg, …) => an_array_of_array

zip(arg, …) { |arr| block } => nil

ex

a = [4, 5, 6]

b = [7, 8, 9]

a.zip(b) #=> [[4, 7], [5, 8], [6, 9]]

[1, 2, 3].zip(a, b) #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]

[1, 2].zip(a, b) #=> [[1, 4, 7], [2, 5, 8]]

a.zip([1, 2], [8]) #=> [[4, 1, 8], [5, 2, nil], [6, nil, nil]]

c = []

a.zip(b) { |x, y| c nil

c #=> [11, 13, 15]

19
Q

Ruby Enumerable

iterates the given block for each element with an arbitrary object given, and returns the initially given object

A

each_with_object(obj) { |element, object| block } => obj

ex

evens = (1..10).each_with_object([]) { |i, a| a [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

20
Q

Ruby Enumerable

all? { |obj| block } => true or false

A

returns true if the block never returns false or nil

21
Q

Ruby Enumerable

any? { |obj| block } => true or false

A

returns true if the block ever returns a value other than false or nil

22
Q

Ruby Enumerable

map { |obj| block } => array

A

returns a new array with the results of running block once for every element in enum

23
Q

Ruby Enumerable

count { |obj| block } => int

A

returns the number of elements for which the block yields a true value

24
Q

Ruby Enumerable

find(ifnone = nil) { |obj| block } => obj or nil

A

returns the first for which block is not false

25
Q

Ruby Enumerable

find_all { |obj| block } => array

A

alias for select

26
Q

Ruby Enumerable

group_by { |obj| block } => a_hash

ex

(1..6).group_by { |i| i%3 } #=> {0=>[3, 6], 1=>[1, 4], 2=>[2, 5]}

A

groups the collection by result of the block; returns a hash where the keys are the evaluated result from the block and the values are arrays of elements in the collection that correspond to the key

27
Q

Ruby Enumerable

include?(obj) => true or false

A

returns true if any member of enum equals obj

28
Q

Ruby Enumerable

inject(initial, sym) => obj inject(initial) { |memo, obj| block } => obj

ex

(5..10).inject(0, &:+) => 45 (5..10).inject(0) { |memo, num| memo + num } => 45

A

combines all elements of enum by applying a binary operation, specified by a block or a symbol that names a method or operator

29
Q

Ruby Enumerable

max(n) { |a,b| block } => obj

ex

a = %w(albatross dog horse)

a. max #=> “horse”
a. max { |a, b| a.length b.length } #=> “albatross”

A

returns the object in enum with the maximum value; can use a block to implement a \<=\> b

30
Q

Ruby Enumerable

member?(obj) => true or false

A

alias of include?

31
Q

Ruby Enumerable

min(n) { |a,b| block }

A

opposite of max

32
Q

Ruby Enumerable

none? { |obj| block } => true or false

A

returns true if the block never returns true for all elements

33
Q

Ruby Enumerable

one? { |obj| block } => true or false

A

returns true if the block returns true exactly once

34
Q

Ruby Enumerable

reject { |obj| block } => array

A

opposite of select

35
Q

Ruby Enumerable

select { |obj| block } => array

A

returns an array containing all elements of enum for which the given block returns a true value

36
Q

Ruby Enumerable

sort { |a, b| block } => array

A

returns an array containing the items in enum sorted, either according to their own method, or by using the results of the supplied block

37
Q

Ruby Enumerable

zip(arg, …) => an_array_of_array

zip(arg, …) { |arr| block } => nil

ex

a = [4, 5, 6]

b = [7, 8, 9]

a.zip(b) #=> [[4, 7], [5, 8], [6, 9]]

[1, 2, 3].zip(a, b) #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]

[1, 2].zip(a, b) #=> [[1, 4, 7], [2, 5, 8]]

a.zip([1, 2], [8]) #=> [[4, 1, 8], [5, 2, nil], [6, nil, nil]]

c = []

a.zip(b) { |x, y| c nil

c #=> [11, 13, 15]

A

takes one element from enum and merges corresponding elements from each args. This generates a sequence of n-element arrays, where n is one more than the count of arguments

38
Q

Ruby Enumerable

each_with_object(obj) { |element, object| block } => obj

ex

evens = (1..10).each_with_object([]) { |i, a| a [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

A

iterates the given block for each element with an arbitrary object given, and returns the initially given object