4 # The <code>Enumerable</code> mixin provides collection classes with
5 # several traversal and searching methods, and with the ability to
6 # sort. The class must provide a method `each`, which
7 # yields successive members of the collection. If
8 # {Enumerable#max}, {#min}, or
9 # {#sort} is used, the objects in the collection must also
10 # implement a meaningful `<=>` operator, as these methods
11 # rely on an ordering between members of the collection.
17 # Call the given block for each element
18 # which is yield by +each+. Return false
19 # if one block value is false. Otherwise
20 # return true. If no block is given and
21 # +self+ is false return false.
26 self.each{|*val| return false unless block.call(*val)}
28 self.each{|*val| return false unless val.__svalue}
34 # Call the given block for each element
35 # which is yield by +each+. Return true
36 # if one block value is true. Otherwise
37 # return false. If no block is given and
38 # +self+ is true object return true.
43 self.each{|*val| return true if block.call(*val)}
45 self.each{|*val| return true if val.__svalue}
51 # Call the given block for each element
52 # which is yield by +each+. Append all
53 # values of each block together and
58 return to_enum :collect unless block
61 self.each{|*val| ary.push(block.call(*val))}
66 # Call the given block for each element
67 # which is yield by +each+. Return
68 # +ifnone+ if no block value was true.
69 # Otherwise return the first block value
73 def detect(ifnone=nil, &block)
85 # Call the given block for each element
86 # which is yield by +each+. Pass an
87 # index to the block which starts at 0
88 # and increase by 1 for each element.
91 def each_with_index(&block)
92 return to_enum :each_with_index unless block
96 block.call(val.__svalue, i)
103 # Return an array of all elements which
104 # are yield by +each+.
110 # __svalue is an internal method
111 ary.push val.__svalue
123 # Call the given block for each element
124 # which is yield by +each+. Return an array
125 # which contains all elements whose block
130 return to_enum :find_all unless block
134 ary.push(val.__svalue) if block.call(*val)
140 # Call the given block for each element
141 # which is yield by +each+ and which return
142 # value was true when invoking === with
143 # +pattern+. Return an array with all
144 # elements or the respective block values.
147 def grep(pattern, &block)
152 ary.push((block)? block.call(*val): sv)
159 # Return true if at least one element which
160 # is yield by +each+ returns a true value
161 # by invoking == with +obj+. Otherwise return
167 return true if val.__svalue == obj
173 # Call the given block for each element
174 # which is yield by +each+. Return value
175 # is the sum of all block values. Pass
176 # to each block the current sum and the
180 def inject(*args, &block)
181 raise ArgumentError, "too many arguments" if args.size > 2
182 if Symbol === args[-1]
184 block = ->(x,y){x.__send__(sym,y)}
188 flag = true # no initial argument
197 # push first element as initial
201 result = block.call(result, val)
215 # Return the maximum value of all elements
216 # yield by +each+. If no block is given <=>
217 # will be invoked to define this value. If
218 # a block is given it will be used instead.
222 flag = true # 1st element?
232 result = val if block.call(val, result) > 0
234 result = val if (val <=> result) > 0
242 # Return the minimum value of all elements
243 # yield by +each+. If no block is given <=>
244 # will be invoked to define this value. If
245 # a block is given it will be used instead.
249 flag = true # 1st element?
259 result = val if block.call(val, result) < 0
261 result = val if (val <=> result) < 0
272 alias member? include?
275 # Call the given block for each element
276 # which is yield by +each+. Return an
277 # array which contains two arrays. The
278 # first array contains all elements
279 # whose block value was true. The second
280 # array contains all elements whose
281 # block value was false.
284 def partition(&block)
289 ary_T.push(val.__svalue)
291 ary_F.push(val.__svalue)
298 # Call the given block for each element
299 # which is yield by +each+. Return an
300 # array which contains only the elements
301 # whose block value was false.
307 ary.push(val.__svalue) unless block.call(*val)
313 # Alias for find_all.
316 alias select find_all
319 # Return a sorted array of all elements
320 # which are yield by +each+. If no block
321 # is given <=> will be invoked on each
322 # element to define the order. Otherwise
323 # the given block will be used for
328 self.map{|*val| val.__svalue}.sort
337 # redefine #hash 15.3.1.3.15
342 n = (e.hash & (0x7fffffff >> (i % 16))) << (i % 16)