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 }