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

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

find_all

enum.find_all { |obj| block } â arrayenum.find_all â an_enumerator Instance Public methods Returns an array containing all elements of enum for which the given block returns a true value. If no block is given, an Enumerator is returned instead. (1..10).find_all { |i| i % 3 == 0 } #=> [3, 6, 9] [1,2,3,4,5].select { |num| num.even? } #=> [2, 4] See also #reject.

find

enum.find(ifnone = nil) { |obj| block } â obj or nilenum.find(ifnone = nil) â an_enumerator Instance Public methods Passes each entry in enum to block. Returns the first for which block is not false. If no object matches, calls ifnone and returns its result when it is specified, or returns nil otherwise. If no block is given, an enumerator is returned instead. (1..10).detect { |i| i % 5 == 0 and i % 7 == 0 } #=> nil (1..100).find { |i| i % 5 == 0 and

entries

enum.entries â array Instance Public methods Returns an array containing the items in enum. (1..7).to_a #=> [1, 2, 3, 4, 5, 6, 7] { 'a'=>1, 'b'=>2, 'c'=>3 }.to_a #=> [["a", 1], ["b", 2], ["c", 3]]

each_with_object

enum.each_with_object(obj) { |(*args), memo_obj| ... } â objenum.each_with_object(obj) â an_enumerator Instance Public methods Iterates the given block for each element with an arbitrary object given, and returns the initially given object. If no block is given, returns an enumerator. evens = (1..10).each_with_object([]) { |i, a| a << i*2 } #=> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

each_with_index

enum.each_with_index(*args) { |obj, i| block } â enumenum.each_with_index(*args) â an_enumerator Instance Public methods Calls block with two arguments, the item and its index, for each item in enum. Given arguments are passed through to each(). If no block is given, an enumerator is returned instead. hash = Hash.new %w(cat dog wombat).each_with_index { |item, index| hash[item] = index } hash #=> {"cat"=>0, "dog"=>1, "wombat"=>2}

each_slice

enum.each_slice(n) { ... } â nilenum.each_slice(n) â an_enumerator Instance Public methods Iterates the given block for each slice of <n> elements. If no block is given, returns an enumerator. (1..10).each_slice(3) { |a| p a } # outputs below [1, 2, 3] [4, 5, 6] [7, 8, 9] [10]

each_entry

enum.each_entry { |obj| block } â enumenum.each_entry â an_enumerator Instance Public methods Calls block once for each element in self, passing that element as a parameter, converting multiple values from yield to an array. If no block is given, an enumerator is returned instead. class Foo include Enumerable def each yield 1 yield 1, 2 yield end end Foo.new.each_entry{ |o| p o } produces: 1 [1, 2] nil

each_cons

enum.each_cons(n) { ... } â nilenum.each_cons(n) â an_enumerator Instance Public methods Iterates the given block for each array of consecutive <n> elements. If no block is given, returns an enumerator. e.g.: (1..10).each_cons(3) { |a| p a } # outputs below [1, 2, 3] [2, 3, 4] [3, 4, 5] [4, 5, 6] [5, 6, 7] [6, 7, 8] [7, 8, 9] [8, 9, 10]