Enumerable Methods
The following index of Enumerable methods are called on an Array with the following definition:
items = ['this', 'thing', 'the', 'other', 'thing']
Presence and Count
Check for Inclusion
items.member? 'this' # true
items.member? 'grape' # false
items.include? 'this' # true
items.include? 'grape' # false
Check for at least one
items.any? { |i| i.class === String } # true
items.any? { |i| i === 'grape' } # false
Check for exactly one
items.one? { |i| i === 'this' } # true
items.one? { |i| i === 'thing' } # false
Check for omnipresence
items.all? { |i| i.class === String } # true
items.all? { |i| i === 'this' } # false
Check for non-presence
items.none? { |i| i.class === Integer } # true
items.none? { |i| i.class === String } # false
Count the number of elements
items.count # 5
items.count { |i| i === 'thing' } # 2
Get a count of each element's occurrence
items.tally # { "this"=>1, "thing"=>2, "the"=>1, "other"=>1 }
Return an array
items.to_a # ["this", "thing", "the", "other", "thing"]
Querying
Return the first element
items.first # "this"
Return the last element
items.last # "thing"
Return some number of leading elements
items.take(2) # ["this", "thing"]
Return some number of trailing elements
items.drop(2) # ["other", "thing"]
Return leading elements until a given condition returns false
items.take_while { |i| [4,5].include? i.length } # ["this", "thing"]
Return trailing elements until a given condition returns false
items.drop_while { |i| [4,5].include? i.length } # ["the", "other", "thing"]
Return minimum valued elements as determined by <=>
items.min # "other"
items.min { |a,b| a.size <=> b.size } # "the"
Return maximum valued elements as determined by <=>
items.max # "this"
items.max { |a,b| a.size <=> b.size } # "thing"
Return the smallest element as specified
items.min_by { |i| i.length } # "the"
Return the largest element as specified
items.max_by { |i| i.length } # "thing"
Return the smallest and largest elements
items.minmax # ["other", "this"]
Return the smallest and largest elements as specified
items.minmax_by { |i| i.length } # ["the", "thing"]
Return a hash of elements grouped as specified
items.group_by { |i| i[0] } # { "t"=>["this", "thing", "the", "thing"], "o"=>["other"] }
Return a two-dimensional array partitioned as specified
items.partition { |i| i[0] === 't' } # [["this", "thing", "the", "thing"], ["other"]]
Return an enumerator of entries partitioned as specified
items.slice_before { |i| i === 'thing' } # #<Enumerator:...>
items.slice_before { |i| i === 'thing' }.to_a # [["this"], ["thing", "the", "other"], ["thing"]]
items.slice_after { |i| i === 'thing' } # #<Enumerator:...>
items.slice_after { |i| i === 'thing' }.to_a # [["this", "thing"], ["the", "other", "thing"]]
Return an enumerator of entries grouped as specified
items.chunk { |i| i[0] } # #<Enumerator: ...>
items.chunk { |i| i[0] }.to_a # [["t", ["this", "thing", "the"]], ["o", ["other"]], ["t", ["thing"]]]
Return an enumerator sequentially grouped as specified
items.chunk_while { |a,b| a[0] == b[0] } # #<Enumerator:...>
items.chunk_while { |a,b| a[0] == b[0] }.to_a # [["this", "thing", "the"], ["other"], ["thing"]]
Search and Filter
Return the first matching element
items.find { |i| i == 'thing' } # "thing"
items.find { |i| i == 'grape' } # nil
items.detect { |i| i == 'thing' } # "thing"
items.detect { |i| i == 'grape' } # nil
Return multiple matching elements as specified
items.find_all { |i| i == 'thing' } # ["thing","thing"]
items.find_all { |i| i == 'grape' } # []
Return the index of the first matching element
items.find_index { |i| i == 'thing' } # 1
items.find_index { |i| i == 'grape' } # nil
Return an array of elements not rejected by the condition
items.reject { |i| i[0] == 't' } # ["other"]
Return an array of elements without duplicates
items.uniq # ["this", "thing", "the", "other"]
Return an array of elements matching the specified pattern
items.grep(String) # ["this", "thing", "the", "other", "thing"]
items.grep(/thing/) # ["thing", "thing"]
Return an array of elements not matching the specified pattern
items.grep_v(String) # []
items.grep_v(/thing/) # ["this", "the", "other"]
Sorting
Return the elements sorted by <=> or as specified
items.sort # ["other", "the", "thing", "thing", "this"]
items.sort { |a,b| a[a.size-1] <=> b[b.size-1] } # ["the", "thing", "thing", "other", "this"]
Return the elements sorted as specified
items.sort_by { |i| i[i.size - 1] } # ["the", "thing", "thing", "other", "this"]
Iteration
Call a block with each successive element
items.each_entry { |i| print i } # prints "this" then "thing" et cetera
Call a block with each successive element and its index
items.each_with_index { |i, idx| print "#{i} is at index #{idx}"} # prints "this is at index 0" et cetera
Call a block with each successive element and an object
items.each_with_object([]) { |i, obj| obj << i[0..1] } # ["th", "th", "th", "ot", "th"]
Make an enumerable of entries partitioned by size and call a block with each partition
items.each_slice(2) { |s| print s[0] } # prints "this" then prints "the" et cetera
Make an enumerable of entries partitioned by size starting at each element and call a block with each partition
items.each_cons(2) { |p| print p[0] } # prints "this" then prints "thing" et cetera
Call a block with each successive element in reverse order
items.reverse_each { |i| print i } # prints "thing" then print "other" et cetera
Return an array of values by processing each entry as specified
items.map { |i| i.upcase } # ["THIS", "THING", "THE", "OTHER", "THING"]
items.collect { |i| i.upcase } # ["THIS", "THING", "THE", "OTHER", "THING"]
Return an array of values which evaluate to truthy after processing each entry as specified
items.filter_map { |i| i[3] } # ["s", "n", "e", "n"]
Return a single-dimensional array after processing each element as specified
items.flat_map { |i| [i, i.upcase] } # ["this", "THIS", "thing", "THING", "the", "THE", "other", "OTHER", "thing", "THING"]
Return the object formed by combining elements as specified
items.reduce('') { |obj, i| obj + i } # "thisthingtheotherthing"
items.inject('') { |obj, i| obj + i } # "thisthingtheotherthing"
Return a summation of elements processed as specified and summed using "+"
items.sum('') # "thisthingtheotherthing"
items.sum('') { |i| i[0] == 't' ? i : '' } # "thisthingthething"
Return an array of with each index being a combination of the elements of each array at the index
items.zip # [["this"], ["thing"], ["the"], ["other"], ["thing"]]
items.zip([0,1,2,3]) # [["this", 0], ["thing", 1], ["the", 2], ["other", 3], ["thing", nil]]
Call a given block for each entry a specified number of times
items.cycle(2) { |i| print i[0] } # prints "t" then "t" then "t" then "o" then "t", two times