7 # enum.drop(n) -> array
9 # Drops first n elements from <i>enum</i>, and returns rest elements
12 # a = [1, 2, 3, 4, 5, 0]
13 # a.drop(3) #=> [4, 5, 0]
16 raise TypeError, "expected Integer for 1st argument" unless n.kind_of? Integer
17 raise ArgumentError, "attempt to drop negative size" if n < 0
20 self.each {|*val| n == 0 ? ary << val.__svalue : n -= 1 }
26 # enum.drop_while {|arr| block } -> array
28 # Drops elements up to, but not including, the first element for
29 # which the block returns +nil+ or +false+ and returns an array
30 # containing the remaining elements.
32 # a = [1, 2, 3, 4, 5, 0]
33 # a.drop_while {|i| i < 3 } #=> [3, 4, 5, 0]
35 def drop_while(&block)
36 ary, state = [], false
38 state = true if !state and !block.call(*val)
39 ary << val.__svalue if state
46 # enum.take(n) -> array
48 # Returns first n elements from <i>enum</i>.
50 # a = [1, 2, 3, 4, 5, 0]
51 # a.take(3) #=> [1, 2, 3]
54 raise TypeError, "expected Integer for 1st argument" unless n.kind_of? Integer
55 raise ArgumentError, "attempt to take negative size" if n < 0
59 break if ary.size >= n
67 # enum.take_while {|arr| block } -> array
69 # Passes elements to the block until the block returns +nil+ or +false+,
70 # then stops iterating and returns an array of all prior elements.
73 # a = [1, 2, 3, 4, 5, 0]
74 # a.take_while {|i| i < 3 } #=> [1, 2]
76 def take_while(&block)
79 return ary unless block.call(*val)
87 # enum.each_cons(n) {...} -> nil
89 # Iterates the given block for each array of consecutive <n>
93 # (1..10).each_cons(3) {|a| p a}
104 def each_cons(n, &block)
105 raise TypeError, "expected Integer for 1st argument" unless n.kind_of? Integer
106 raise ArgumentError, "invalid size" if n <= 0
110 ary.shift if ary.size == n
112 block.call(ary.dup) if ary.size == n
118 # enum.each_slice(n) {...} -> nil
120 # Iterates the given block for each slice of <n> elements.
123 # (1..10).each_slice(3) {|a| p a}
130 def each_slice(n, &block)
131 raise TypeError, "expected Integer for 1st argument" unless n.kind_of? Integer
132 raise ArgumentError, "invalid slice size" if n <= 0
142 block.call(ary) unless ary.empty?
147 # enum.group_by {| obj | block } -> a_hash
149 # Returns a hash, which keys are evaluated result from the
150 # block, and values are arrays of elements in <i>enum</i>
151 # corresponding to the key.
153 # (1..6).group_by {|i| i%3} #=> {0=>[3, 6], 1=>[1, 4], 2=>[2, 5]}
158 key = block.call(*val)
160 h.key?(key) ? (h[key] << sv) : (h[key] = [sv])
167 # enum.sort_by { |obj| block } -> array
169 # Sorts <i>enum</i> using a set of keys generated by mapping the
170 # values in <i>enum</i> through the given block.
174 self.each_with_index{|e, i|
176 ary.push([block.call(e), i])
179 __sort_sub__(ary, ::Array.new(ary.size), 0, 0, ary.size - 1) do |a,b|
183 ary.collect{|e,i| orig[i]}
189 # enum.first -> obj or nil
190 # enum.first(n) -> an_array
192 # Returns the first element, or the first +n+ elements, of the enumerable.
193 # If the enumerable is empty, the first form returns <code>nil</code>, and the
194 # second form returns an empty array.
216 # enum.count(item) -> int
217 # enum.count { |obj| block } -> int
219 # Returns the number of items in +enum+ through enumeration.
220 # If an argument is given, the number of items in +enum+ that
221 # are equal to +item+ are counted. If a block is given, it
222 # counts the number of elements yielding a true value.
223 def count(v=NONE, &block)
227 count += 1 if block.call(*val)
231 self.each { count += 1 }
234 count += 1 if val.__svalue == v
243 # enum.flat_map { |obj| block } -> array
244 # enum.collect_concat { |obj| block } -> array
245 # enum.flat_map -> an_enumerator
246 # enum.collect_concat -> an_enumerator
248 # Returns a new array with the concatenated results of running
249 # <em>block</em> once for every element in <i>enum</i>.
251 # If no block is given, an enumerator is returned instead.
253 # [1, 2, 3, 4].flat_map { |e| [e, -e] } #=> [1, -1, 2, -2, 3, -3, 4, -4]
254 # [[1, 2], [3, 4]].flat_map { |e| e + [100] } #=> [1, 2, 100, 3, 4, 100]
256 return to_enum :flat_map unless block_given?
261 if e2.respond_to? :each
262 e2.each {|e3| ary.push(e3) }
269 alias collect_concat flat_map
273 # enum.max_by {|obj| block } -> obj
274 # enum.max_by -> an_enumerator
276 # Returns the object in <i>enum</i> that gives the maximum
277 # value from the given block.
279 # If no block is given, an enumerator is returned instead.
281 # %w[albatross dog horse].max_by {|x| x.length } #=> "albatross"
284 return to_enum :max_by unless block_given?
293 max_cmp = block.call(*val)
296 if (cmp = block.call(*val)) > max_cmp
307 # enum.min_by {|obj| block } -> obj
308 # enum.min_by -> an_enumerator
310 # Returns the object in <i>enum</i> that gives the minimum
311 # value from the given block.
313 # If no block is given, an enumerator is returned instead.
315 # %w[albatross dog horse].min_by {|x| x.length } #=> "dog"
318 return to_enum :min_by unless block_given?
327 min_cmp = block.call(*val)
330 if (cmp = block.call(*val)) < min_cmp
341 # enum.minmax -> [min, max]
342 # enum.minmax { |a, b| block } -> [min, max]
344 # Returns two elements array which contains the minimum and the
345 # maximum value in the enumerable. The first form assumes all
346 # objects implement <code>Comparable</code>; the second uses the
347 # block to return <em>a <=> b</em>.
349 # a = %w(albatross dog horse)
350 # a.minmax #=> ["albatross", "horse"]
351 # a.minmax { |a, b| a.length <=> b.length } #=> ["dog", "albatross"]
366 max = val.__svalue if block.call(*val, max) > 0
367 min = val.__svalue if block.call(*val, min) < 0
370 max = val if (val <=> max) > 0
371 min = val if (val <=> min) < 0
380 # enum.minmax_by { |obj| block } -> [min, max]
381 # enum.minmax_by -> an_enumerator
383 # Returns a two element array containing the objects in
384 # <i>enum</i> that correspond to the minimum and maximum values respectively
385 # from the given block.
387 # If no block is given, an enumerator is returned instead.
389 # %w(albatross dog horse).minmax_by { |x| x.length } #=> ["dog", "albatross"]
391 def minmax_by(&block)
400 max = min = val.__svalue
401 max_cmp = min_cmp = block.call(*val)
404 if (cmp = block.call(*val)) > max_cmp
408 if (cmp = block.call(*val)) < min_cmp
419 # enum.none? [{ |obj| block }] -> true or false
421 # Passes each element of the collection to the given block. The method
422 # returns <code>true</code> if the block never returns <code>true</code>
423 # for all elements. If the block is not given, <code>none?</code> will return
424 # <code>true</code> only if none of the collection members is true.
426 # %w(ant bear cat).none? { |word| word.length == 5 } #=> true
427 # %w(ant bear cat).none? { |word| word.length >= 4 } #=> false
429 # [nil, false].none? #=> true
430 # [nil, true].none? #=> false
435 return false if block.call(*val)
439 return false if val.__svalue
447 # enum.one? [{ |obj| block }] -> true or false
449 # Passes each element of the collection to the given block. The method
450 # returns <code>true</code> if the block returns <code>true</code>
451 # exactly once. If the block is not given, <code>one?</code> will return
452 # <code>true</code> only if exactly one of the collection members is
455 # %w(ant bear cat).one? { |word| word.length == 4 } #=> true
456 # %w(ant bear cat).one? { |word| word.length > 4 } #=> false
457 # %w(ant bear cat).one? { |word| word.length < 4 } #=> false
458 # [nil, true, 99].one? #=> false
459 # [nil, true, false].one? #=> true
466 count += 1 if block.call(*val)
467 return false if count > 1
471 count += 1 if val.__svalue
472 return false if count > 1
476 count == 1 ? true : false
481 # enum.each_with_object(obj) { |(*args), memo_obj| ... } -> obj
482 # enum.each_with_object(obj) -> an_enumerator
484 # Iterates the given block for each element with an arbitrary
485 # object given, and returns the initially given object.
487 # If no block is given, returns an enumerator.
489 # (1..10).each_with_object([]) { |i, a| a << i*2 }
490 # #=> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
493 def each_with_object(obj=nil, &block)
494 raise ArgumentError, "wrong number of arguments (0 for 1)" if obj == nil
496 return to_enum :each_with_object unless block_given?
498 self.each {|*val| block.call(val.__svalue, obj) }
504 # enum.reverse_each { |item| block } -> enum
505 # enum.reverse_each -> an_enumerator
507 # Builds a temporary array and traverses that array in reverse order.
509 # If no block is given, an enumerator is returned instead.
511 # (1..3).reverse_each { |v| p v }
520 def reverse_each(&block)
522 self.each {|*val| ary.push(val.__svalue) }
523 ary.reverse_each(&block)