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.
19 # Call the given block for each element
20 # which is yield by +each+. Return false
21 # if one block value is false. Otherwise
22 # return true. If no block is given and
23 # +self+ is false return false.
28 self.each{|*val| return false unless block.call(*val)}
30 self.each{|*val| return false unless val.__svalue}
36 # Call the given block for each element
37 # which is yield by +each+. Return true
38 # if one block value is true. Otherwise
39 # return false. If no block is given and
40 # +self+ is true object return true.
45 self.each{|*val| return true if block.call(*val)}
47 self.each{|*val| return true if val.__svalue}
53 # Call the given block for each element
54 # which is yield by +each+. Append all
55 # values of each block together and
60 return to_enum :collect unless block
63 self.each{|*val| ary.push(block.call(*val))}
68 # Return the first element for which
69 # value from the block is true. If no
70 # object matches, calls +ifnone+ and
71 # returns its result. Otherwise returns
75 def detect(ifnone=nil, &block)
76 return to_enum :detect, ifnone unless block
83 ifnone.call unless ifnone.nil?
87 # Call the given block for each element
88 # which is yield by +each+. Pass an
89 # index to the block which starts at 0
90 # and increase by 1 for each element.
93 def each_with_index(&block)
94 return to_enum :each_with_index unless block
98 block.call(val.__svalue, i)
105 # Return an array of all elements which
106 # are yield by +each+.
112 # __svalue is an internal method
113 ary.push val.__svalue
125 # Call the given block for each element
126 # which is yield by +each+. Return an array
127 # which contains all elements whose block
132 return to_enum :find_all unless block
136 ary.push(val.__svalue) if block.call(*val)
142 # Call the given block for each element
143 # which is yield by +each+ and which return
144 # value was true when invoking === with
145 # +pattern+. Return an array with all
146 # elements or the respective block values.
149 def grep(pattern, &block)
154 ary.push((block)? block.call(*val): sv)
161 # Return true if at least one element which
162 # is yield by +each+ returns a true value
163 # by invoking == with +obj+. Otherwise return
169 return true if val.__svalue == obj
175 # Call the given block for each element
176 # which is yield by +each+. Return value
177 # is the sum of all block values. Pass
178 # to each block the current sum and the
182 def inject(*args, &block)
183 raise ArgumentError, "too many arguments" if args.size > 2
184 if Symbol === args[-1]
186 block = ->(x,y){x.__send__(sym,y)}
190 flag = true # no initial argument
199 # push first element as initial
203 result = block.call(result, val)
217 # Return the maximum value of all elements
218 # yield by +each+. If no block is given <=>
219 # will be invoked to define this value. If
220 # a block is given it will be used instead.
224 flag = true # 1st element?
234 result = val if block.call(val, result) > 0
236 result = val if (val <=> result) > 0
244 # Return the minimum value of all elements
245 # yield by +each+. If no block is given <=>
246 # will be invoked to define this value. If
247 # a block is given it will be used instead.
251 flag = true # 1st element?
261 result = val if block.call(val, result) < 0
263 result = val if (val <=> result) < 0
274 alias member? include?
277 # Call the given block for each element
278 # which is yield by +each+. Return an
279 # array which contains two arrays. The
280 # first array contains all elements
281 # whose block value was true. The second
282 # array contains all elements whose
283 # block value was false.
286 def partition(&block)
287 return to_enum :partition unless block
293 ary_T.push(val.__svalue)
295 ary_F.push(val.__svalue)
302 # Call the given block for each element
303 # which is yield by +each+. Return an
304 # array which contains only the elements
305 # whose block value was false.
309 return to_enum :reject unless block
313 ary.push(val.__svalue) unless block.call(*val)
319 # Alias for find_all.
322 alias select find_all
325 # Return a sorted array of all elements
326 # which are yield by +each+. If no block
327 # is given <=> will be invoked on each
328 # element to define the order. Otherwise
329 # the given block will be used for
334 self.map{|*val| val.__svalue}.sort(&block)
343 # redefine #hash 15.3.1.3.15
348 h = __update_hash(h, i, e.hash)