find_index

enum.find_index(value) â int or nilenum.find_index { |obj| block } â int or nilenum.find_index â an_enumerator Instance Public methods Compares each entry in enum with value or passes to block. Returns the index for the first for which the evaluated value is non-false. If no object matches, returns nil If neither block nor argument is given, an enumerator is returned instead. (1..10).find_index { |i| i % 5 == 0 and i % 7 == 0 } #=> nil (1..100).find

first

enum.first â obj or nilenum.first(n) â an_array Instance Public methods Returns the first element, or the first n elements, of the enumerable. If the enumerable is empty, the first form returns nil, and the second form returns an empty array. %w[foo bar baz].first #=> "foo" %w[foo bar baz].first(2) #=> ["foo", "bar"] %w[foo bar baz].first(10) #=> ["foo", "bar", "baz"] [].first #=> nil

flat_map

enum.flat_map { |obj| block } â arrayenum.flat_map â an_enumerator Instance Public methods Returns a new array with the concatenated results of running block once for every element in enum. If no block is given, an enumerator is returned instead. [1, 2, 3, 4].flat_map { |e| [e, -e] } #=> [1, -1, 2, -2, 3, -3, 4, -4] [[1, 2], [3, 4]].flat_map { |e| e + [100] } #=> [1, 2, 100, 3, 4, 100]

grep

enum.grep(pattern) â arrayenum.grep(pattern) { |obj| block } â array Instance Public methods Returns an array of every element in enum for which Pattern === element. If the optional block is supplied, each matching element is passed to it, and the block's result is stored in the output array. (1..100).grep 38..44 #=> [38, 39, 40, 41, 42, 43, 44] c = IO.constants c.grep(/SEEK/) #=> [:SEEK_SET, :SEEK_CUR, :SEEK_END] res = c.grep(/SEEK/) { |v| IO.con

group_by

enum.group_by { |obj| block } â a_hashenum.group_by â an_enumerator Instance Public methods 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. If no block is given an enumerator is returned. (1..6).group_by { |i| i%3 } #=> {0=>[3, 6], 1=>[1, 4], 2=>[2, 5]}

include?

enum.include?(obj) â true or false Instance Public methods Returns true if any member of enum equals obj. Equality is tested using ==. IO.constants.include? :SEEK_SET #=> true IO.constants.include? :SEEK_NO_FURTHER #=> false

inject

enum.inject(initial, sym) â objenum.inject(sym) â objenum.inject(initial) { |memo, obj| block } â objenum.inject { |memo, obj| block } â obj Instance Public methods Combines all elements of enum by applying a binary operation, specified by a block or a symbol that names a method or operator. If you specify a block, then for each element in enum the block is passed an accumulator value (memo) and the element. If you specify a symbol instead, then each element in

lazy

e.lazy â lazy_enumerator Instance Public methods Returns a lazy enumerator, whose methods map/collect, flat_map/collect_concat, select/find_all, reject, grep, zip, take, #take_while, drop, and #drop_while enumerate values only on an as-needed basis. However, if a block is given to zip, values are enumerated immediately. Example The following program finds pythagorean triples: def pythagorean_triples (1..Float::INFINITY).lazy.flat_map {|z| (1..z).flat_map {|x| (x..z).s

map

enum.map { |obj| block } â arrayenum.map â an_enumerator Instance Public methods Returns a new array with the results of running block once for every element in enum. If no block is given, an enumerator is returned instead. (1..4).collect { |i| i*i } #=> [1, 4, 9, 16] (1..4).collect { "cat" } #=> ["cat", "cat", "cat", "cat"]

max

enum.max â objenum.max { |a, b| block } â obj Instance Public methods Returns the object in enum with the maximum value. The first form assumes all objects implement Comparable; the second uses the block to return a <=> b. a = %w(albatross dog horse) a.max #=> "horse" a.max { |a, b| a.length <=> b.length } #=> "albatross"