drop_while

enum.drop_while { |arr| block } â arrayenum.drop_while â an_enumerator Instance Public methods Drops elements up to, but not including, the first element for which the block returns nil or false and returns an array containing the remaining elements. If no block is given, an enumerator is returned instead. a = [1, 2, 3, 4, 5, 0] a.drop_while { |i| i < 3 } #=> [3, 4, 5, 0]

drop

enum.drop(n) â array Instance Public methods Drops first n elements from enum, and returns rest elements in an array. a = [1, 2, 3, 4, 5, 0] a.drop(3) #=> [4, 5, 0]

detect

enum.detect(ifnone = nil) { |obj| block } â obj or nilenum.detect(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

cycle

enum.cycle(n=nil) { |obj| block } â nilenum.cycle(n=nil) â an_enumerator Instance Public methods Calls block for each element of enum repeatedly n times or forever if none or nil is given. If a non-positive number is given or the collection is empty, does nothing. Returns nil if the loop has finished without getting interrupted. #cycle saves elements in an internal array so changes to enum after the first pass have no effect. If no block is given, an enumerat

count

enum.count â intenum.count(item) â intenum.count { |obj| block } â int Instance Public methods Returns the number of items in enum through enumeration. If an argument is given, the number of items in enum that are equal to item are counted. If a block is given, it counts the number of elements yielding a true value. ary = [1, 2, 4, 2] ary.count #=> 4 ary.count(2) #=> 2 ary.count{ |x| x%2==0 } #=> 3

collect_concat

enum.collect_concat { |obj| block } â arrayenum.collect_concat â 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]

collect

enum.collect { |obj| block } â arrayenum.collect â 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"]

chunk

enum.chunk { |elt| ... } â an_enumeratorenum.chunk(initial_state) { |elt, state| ... } â an_enumerator Instance Public methods Enumerates over the items, chunking them together based on the return value of the block. Consecutive elements which return the same block value are chunked together. For example, consecutive even numbers and odd numbers can be chunked as follows. [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5].chunk { |n| n.even? }.each { |even, ary| p [even,

any?

enum.any? [{ |obj| block }] â true or false Instance Public methods Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil. If the block is not given, Ruby adds an implicit block of { |obj| obj } that will cause any? to return true if at least one of the collection members is not false or nil. %w[ant bear cat].any? { |word| word.length >= 3 } #=> true %w[ant bear cat].any? { |word| word.le

all?

enum.all? [{ |obj| block } ] â true or false Instance Public methods Passes each element of the collection to the given block. The method returns true if the block never returns false or nil. If the block is not given, Ruby adds an implicit block of { |obj| obj } which will cause all? to return true when none of the collection members are false or nil. %w[ant bear cat].all? { |word| word.length >= 3 } #=> true %w[ant bear cat].all? { |word| word.length >= 4 } #=> fal