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]
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
27 # enum.drop_while -> an_enumerator
29 # Drops elements up to, but not including, the first element for
30 # which the block returns +nil+ or +false+ and returns an array
31 # containing the remaining elements.
33 # If no block is given, an enumerator is returned instead.
35 # a = [1, 2, 3, 4, 5, 0]
36 # a.drop_while {|i| i < 3 } #=> [3, 4, 5, 0]
38 def drop_while(&block)
39 return to_enum :drop_while unless block
41 ary, state = [], false
43 state = true if !state and !block.call(*val)
44 ary << val.__svalue if state
51 # enum.take(n) -> array
53 # Returns first n elements from <i>enum</i>.
55 # a = [1, 2, 3, 4, 5, 0]
56 # a.take(3) #=> [1, 2, 3]
61 raise ArgumentError, "attempt to take negative size" if i < 0
74 # enum.take_while {|arr| block } -> array
75 # enum.take_while -> an_enumerator
77 # Passes elements to the block until the block returns +nil+ or +false+,
78 # then stops iterating and returns an array of all prior elements.
80 # If no block is given, an enumerator is returned instead.
82 # a = [1, 2, 3, 4, 5, 0]
83 # a.take_while {|i| i < 3 } #=> [1, 2]
85 def take_while(&block)
86 return to_enum :take_while unless block
90 return ary unless block.call(*val)
97 # Iterates the given block for each array of consecutive <n>
103 # (1..10).each_cons(3) {|a| p a}
114 def each_cons(n, &block)
116 raise ArgumentError, "invalid size" if n <= 0
118 return to_enum(:each_cons,n) unless block
122 ary.shift if ary.size == n
124 block.call(ary.dup) if ary.size == n
130 # Iterates the given block for each slice of <n> elements.
135 # (1..10).each_slice(3) {|a| p a}
142 def each_slice(n, &block)
144 raise ArgumentError, "invalid slice size" if n <= 0
146 return to_enum(:each_slice,n) unless block
156 block.call(ary) unless ary.empty?
162 # enum.group_by {| obj | block } -> a_hash
163 # enum.group_by -> an_enumerator
165 # Returns a hash, which keys are evaluated result from the
166 # block, and values are arrays of elements in <i>enum</i>
167 # corresponding to the key.
169 # (1..6).group_by {|i| i%3} #=> {0=>[3, 6], 1=>[1, 4], 2=>[2, 5]}
172 return to_enum :group_by unless block
176 key = block.call(*val)
178 h.key?(key) ? (h[key] << sv) : (h[key] = [sv])
185 # enum.sort_by { |obj| block } -> array
186 # enum.sort_by -> an_enumerator
188 # Sorts <i>enum</i> using a set of keys generated by mapping the
189 # values in <i>enum</i> through the given block.
191 # If no block is given, an enumerator is returned instead.
194 return to_enum :sort_by unless block
198 self.each_with_index{|e, i|
200 ary.push([block.call(e), i])
205 ary.collect{|e,i| orig[i]}
210 # enum.first -> obj or nil
211 # enum.first(n) -> an_array
213 # Returns the first element, or the first +n+ elements, of the enumerable.
214 # If the enumerable is empty, the first form returns <code>nil</code>, and the
215 # second form returns an empty array.
225 raise ArgumentError, "attempt to take negative size" if i < 0
235 raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 0..1)"
242 # enum.count(item) -> int
243 # enum.count { |obj| block } -> int
245 # Returns the number of items in +enum+ through enumeration.
246 # If an argument is given, the number of items in +enum+ that
247 # are equal to +item+ are counted. If a block is given, it
248 # counts the number of elements yielding a true value.
249 def count(v=NONE, &block)
253 count += 1 if block.call(*val)
257 self.each { count += 1 }
260 count += 1 if val.__svalue == v
269 # enum.flat_map { |obj| block } -> array
270 # enum.collect_concat { |obj| block } -> array
271 # enum.flat_map -> an_enumerator
272 # enum.collect_concat -> an_enumerator
274 # Returns a new array with the concatenated results of running
275 # <em>block</em> once for every element in <i>enum</i>.
277 # If no block is given, an enumerator is returned instead.
279 # [1, 2, 3, 4].flat_map { |e| [e, -e] } #=> [1, -1, 2, -2, 3, -3, 4, -4]
280 # [[1, 2], [3, 4]].flat_map { |e| e + [100] } #=> [1, 2, 100, 3, 4, 100]
282 return to_enum :flat_map unless block
287 if e2.respond_to? :each
288 e2.each {|e3| ary.push(e3) }
295 alias collect_concat flat_map
299 # enum.max_by {|obj| block } -> obj
300 # enum.max_by -> an_enumerator
302 # Returns the object in <i>enum</i> that gives the maximum
303 # value from the given block.
305 # If no block is given, an enumerator is returned instead.
307 # %w[albatross dog horse].max_by {|x| x.length } #=> "albatross"
310 return to_enum :max_by unless block
319 max_cmp = block.call(*val)
322 if (cmp = block.call(*val)) > max_cmp
333 # enum.min_by {|obj| block } -> obj
334 # enum.min_by -> an_enumerator
336 # Returns the object in <i>enum</i> that gives the minimum
337 # value from the given block.
339 # If no block is given, an enumerator is returned instead.
341 # %w[albatross dog horse].min_by {|x| x.length } #=> "dog"
344 return to_enum :min_by unless block
353 min_cmp = block.call(*val)
356 if (cmp = block.call(*val)) < min_cmp
367 # enum.minmax -> [min, max]
368 # enum.minmax { |a, b| block } -> [min, max]
370 # Returns two elements array which contains the minimum and the
371 # maximum value in the enumerable. The first form assumes all
372 # objects implement <code>Comparable</code>; the second uses the
373 # block to return <em>a <=> b</em>.
375 # a = %w(albatross dog horse)
376 # a.minmax #=> ["albatross", "horse"]
377 # a.minmax { |a, b| a.length <=> b.length } #=> ["dog", "albatross"]
393 max = val if block.call(val, max) > 0
394 min = val if block.call(val, min) < 0
396 max = val if (val <=> max) > 0
397 min = val if (val <=> min) < 0
406 # enum.minmax_by { |obj| block } -> [min, max]
407 # enum.minmax_by -> an_enumerator
409 # Returns a two element array containing the objects in
410 # <i>enum</i> that correspond to the minimum and maximum values respectively
411 # from the given block.
413 # If no block is given, an enumerator is returned instead.
415 # %w(albatross dog horse).minmax_by { |x| x.length } #=> ["dog", "albatross"]
417 def minmax_by(&block)
418 return to_enum :minmax_by unless block
428 max = min = val.__svalue
429 max_cmp = min_cmp = block.call(*val)
432 if (cmp = block.call(*val)) > max_cmp
436 if (cmp = block.call(*val)) < min_cmp
447 # enum.none? [{ |obj| block }] -> true or false
448 # enum.none?(pattern) -> true or false
450 # Passes each element of the collection to the given block. The method
451 # returns <code>true</code> if the block never returns <code>true</code>
452 # for all elements. If the block is not given, <code>none?</code> will return
453 # <code>true</code> only if none of the collection members is true.
455 # If a pattern is supplied instead, the method returns whether
456 # <code>pattern === element</code> for none of the collection members.
458 # %w(ant bear cat).none? { |word| word.length == 5 } #=> true
459 # %w(ant bear cat).none? { |word| word.length >= 4 } #=> false
460 # %w{ant bear cat}.none?(/d/) #=> true
461 # [1, 3.14, 42].none?(Float) #=> false
463 # [nil, false].none? #=> true
464 # [nil, true].none? #=> false
466 def none?(pat=NONE, &block)
469 return false if pat === val.__svalue
473 return false if block.call(*val)
477 return false if val.__svalue
485 # enum.one? [{ |obj| block }] -> true or false
486 # enum.one?(pattern) -> true or false
488 # Passes each element of the collection to the given block. The method
489 # returns <code>true</code> if the block returns <code>true</code>
490 # exactly once. If the block is not given, <code>one?</code> will return
491 # <code>true</code> only if exactly one of the collection members is
494 # If a pattern is supplied instead, the method returns whether
495 # <code>pattern === element</code> for exactly one collection member.
497 # %w(ant bear cat).one? { |word| word.length == 4 } #=> true
498 # %w(ant bear cat).one? { |word| word.length > 4 } #=> false
499 # %w(ant bear cat).one? { |word| word.length < 4 } #=> false
500 # %w{ant bear cat}.one?(/t/) #=> false
501 # [nil, true, 99].one? #=> false
502 # [nil, true, false].one? #=> true
503 # [ nil, true, 99 ].one?(Integer) #=> true
506 def one?(pat=NONE, &block)
510 count += 1 if pat === val.__svalue
511 return false if count > 1
515 count += 1 if block.call(*val)
516 return false if count > 1
520 count += 1 if val.__svalue
521 return false if count > 1
525 count == 1 ? true : false
530 # enum.all? [{ |obj| block } ] -> true or false
531 # enum.all?(pattern) -> true or false
533 # Passes each element of the collection to the given block. The method
534 # returns <code>true</code> if the block never returns
535 # <code>false</code> or <code>nil</code>. If the block is not given,
536 # Ruby adds an implicit block of <code>{ |obj| obj }</code> which will
537 # cause #all? to return +true+ when none of the collection members are
540 # If a pattern is supplied instead, the method returns whether
541 # <code>pattern === element</code> for every collection member.
543 # %w[ant bear cat].all? { |word| word.length >= 3 } #=> true
544 # %w[ant bear cat].all? { |word| word.length >= 4 } #=> false
545 # %w[ant bear cat].all?(/t/) #=> false
546 # [1, 2i, 3.14].all?(Numeric) #=> true
547 # [nil, true, 99].all? #=> false
549 def all?(pat=NONE, &block)
551 self.each{|*val| return false unless pat === val.__svalue}
553 self.each{|*val| return false unless block.call(*val)}
555 self.each{|*val| return false unless val.__svalue}
562 # enum.any? [{ |obj| block }] -> true or false
563 # enum.any?(pattern) -> true or false
565 # Passes each element of the collection to the given block. The method
566 # returns <code>true</code> if the block ever returns a value other
567 # than <code>false</code> or <code>nil</code>. If the block is not
568 # given, Ruby adds an implicit block of <code>{ |obj| obj }</code> that
569 # will cause #any? to return +true+ if at least one of the collection
570 # members is not +false+ or +nil+.
572 # If a pattern is supplied instead, the method returns whether
573 # <code>pattern === element</code> for any collection member.
575 # %w[ant bear cat].any? { |word| word.length >= 3 } #=> true
576 # %w[ant bear cat].any? { |word| word.length >= 4 } #=> true
577 # %w[ant bear cat].any?(/d/) #=> false
578 # [nil, true, 99].any?(Integer) #=> true
579 # [nil, true, 99].any? #=> true
582 def any?(pat=NONE, &block)
584 self.each{|*val| return true if pat === val.__svalue}
586 self.each{|*val| return true if block.call(*val)}
588 self.each{|*val| return true if val.__svalue}
595 # enum.each_with_object(obj) { |(*args), memo_obj| ... } -> obj
596 # enum.each_with_object(obj) -> an_enumerator
598 # Iterates the given block for each element with an arbitrary
599 # object given, and returns the initially given object.
601 # If no block is given, returns an enumerator.
603 # (1..10).each_with_object([]) { |i, a| a << i*2 }
604 # #=> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
607 def each_with_object(obj, &block)
608 return to_enum(:each_with_object, obj) unless block
610 self.each {|*val| block.call(val.__svalue, obj) }
616 # enum.reverse_each { |item| block } -> enum
617 # enum.reverse_each -> an_enumerator
619 # Builds a temporary array and traverses that array in reverse order.
621 # If no block is given, an enumerator is returned instead.
623 # (1..3).reverse_each { |v| p v }
632 def reverse_each(&block)
633 return to_enum :reverse_each unless block
646 # enum.cycle(n=nil) { |obj| block } -> nil
647 # enum.cycle(n=nil) -> an_enumerator
649 # Calls <i>block</i> for each element of <i>enum</i> repeatedly _n_
650 # times or forever if none or +nil+ is given. If a non-positive
651 # number is given or the collection is empty, does nothing. Returns
652 # +nil+ if the loop has finished without getting interrupted.
654 # Enumerable#cycle saves elements in an internal array so changes
655 # to <i>enum</i> after the first pass have no effect.
657 # If no block is given, an enumerator is returned instead.
659 # a = ["a", "b", "c"]
660 # a.cycle { |x| puts x } # print, a, b, c, a, b, c,.. forever.
661 # a.cycle(2) { |x| puts x } # print, a, b, c, a, b, c.
664 def cycle(nv = nil, &block)
665 return to_enum(:cycle, nv) unless block
681 return nil if ary.empty?
683 while n < 0 || 0 < (n -= 1)
694 # enum.find_index(value) -> int or nil
695 # enum.find_index { |obj| block } -> int or nil
696 # enum.find_index -> an_enumerator
698 # Compares each entry in <i>enum</i> with <em>value</em> or passes
699 # to <em>block</em>. Returns the index for the first for which the
700 # evaluated value is non-false. If no object matches, returns
703 # If neither block nor argument is given, an enumerator is returned instead.
705 # (1..10).find_index { |i| i % 5 == 0 and i % 7 == 0 } #=> nil
706 # (1..100).find_index { |i| i % 5 == 0 and i % 7 == 0 } #=> 34
707 # (1..100).find_index(50) #=> 49
710 def find_index(val=NONE, &block)
711 return to_enum(:find_index, val) if !block && val == NONE
716 return idx if block.call(*e)
721 return idx if e.__svalue == val
730 # enum.zip(arg, ...) -> an_array_of_array
731 # enum.zip(arg, ...) { |arr| block } -> nil
733 # Takes one element from <i>enum</i> and merges corresponding
734 # elements from each <i>args</i>. This generates a sequence of
735 # <em>n</em>-element arrays, where <em>n</em> is one more than the
736 # count of arguments. The length of the resulting sequence will be
737 # <code>enum#size</code>. If the size of any argument is less than
738 # <code>enum#size</code>, <code>nil</code> values are supplied. If
739 # a block is given, it is invoked for each output array, otherwise
740 # an array of arrays is returned.
745 # a.zip(b) #=> [[4, 7], [5, 8], [6, 9]]
746 # [1, 2, 3].zip(a, b) #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
747 # [1, 2].zip(a, b) #=> [[1, 4, 7], [2, 5, 8]]
748 # a.zip([1, 2], [8]) #=> [[4, 1, 8], [5, 2, nil], [6, nil, nil]]
751 # a.zip(b) { |x, y| c << x + y } #=> nil
755 def zip(*arg, &block)
756 result = block ? nil : []
758 unless a.respond_to?(:to_a)
759 raise TypeError, "wrong argument type #{a.class} (must respond to :to_a)"
787 # Returns the result of interpreting <i>enum</i> as a list of
788 # <tt>[key, value]</tt> pairs.
790 # %i[hello world].each_with_index.to_h
791 # # => {:hello => 0, :world => 1}
799 raise TypeError, "wrong element type #{v.class} (expected Array)" unless v.is_a? Array
800 raise ArgumentError, "element has wrong array length (expected 2, was #{v.size})" if v.size != 2
806 raise TypeError, "wrong element type #{v.class} (expected Array)" unless v.is_a? Array
807 raise ArgumentError, "element has wrong array length (expected 2, was #{v.size})" if v.size != 2
819 hash[block.call(v)] ||= v
831 return to_enum(:filter_map) unless blk
845 # enum.tally -> a_hash
847 # Tallys the collection. Returns a hash where the keys are the
848 # elements and the values are numbers of elements in the collection
849 # that correspond to the key.
851 # ["a", "b", "c", "b"].tally #=> {"a"=>1, "b"=>2, "c"=>1}
855 hash[x] = (hash[x]||0)+1