make Enumerable methods to support multiple values; collect, detect,...

make Enumerable methods to support multiple values; collect, detect, each_with_index, find_all, inject, max, min, partition, reject, sort
parent ed2fe640
...@@ -99,7 +99,7 @@ class Array ...@@ -99,7 +99,7 @@ class Array
end end
# internal method to convert multi-value to single value # internal method to convert multi-value to single value
def __to_svalue def __svalue
case self.size case self.size
when 0 when 0
return nil return nil
......
...@@ -81,8 +81,8 @@ module Enumerable ...@@ -81,8 +81,8 @@ module Enumerable
return to_enum :collect unless block_given? return to_enum :collect unless block_given?
ary = [] ary = []
self.each{|val| self.each{|*val|
ary.push(block.call(val)) ary.push(block.call(*val))
} }
ary ary
end end
...@@ -97,9 +97,9 @@ module Enumerable ...@@ -97,9 +97,9 @@ module Enumerable
# ISO 15.3.2.2.4 # ISO 15.3.2.2.4
def detect(ifnone=nil, &block) def detect(ifnone=nil, &block)
ret = ifnone ret = ifnone
self.each{|val| self.each{|*val|
if block.call(val) if block.call(*val)
ret = val ret = val.__svalue
break break
end end
} }
...@@ -115,8 +115,8 @@ module Enumerable ...@@ -115,8 +115,8 @@ module Enumerable
# ISO 15.3.2.2.5 # ISO 15.3.2.2.5
def each_with_index(&block) def each_with_index(&block)
i = 0 i = 0
self.each{|val| self.each{|*val|
block.call(val, i) block.call(val.__svalue, i)
i += 1 i += 1
} }
self self
...@@ -130,8 +130,8 @@ module Enumerable ...@@ -130,8 +130,8 @@ module Enumerable
def entries def entries
ary = [] ary = []
self.each{|*val| self.each{|*val|
# __to_svalue is an internal method # __svalue is an internal method
ary.push val.__to_svalue ary.push val.__svalue
} }
ary ary
end end
...@@ -151,8 +151,8 @@ module Enumerable ...@@ -151,8 +151,8 @@ module Enumerable
# ISO 15.3.2.2.8 # ISO 15.3.2.2.8
def find_all(&block) def find_all(&block)
ary = [] ary = []
self.each{|val| self.each{|*val|
ary.push(val) if block.call(val) ary.push(val.__svalue) if block.call(*val)
} }
ary ary
end end
...@@ -215,7 +215,8 @@ module Enumerable ...@@ -215,7 +215,8 @@ module Enumerable
flag = false flag = false
result = args[0] result = args[0]
end end
self.each{|val| self.each{|*val|
val = val.__svalue
if flag if flag
# push first element as initial # push first element as initial
flag = false flag = false
...@@ -244,7 +245,8 @@ module Enumerable ...@@ -244,7 +245,8 @@ module Enumerable
def max(&block) def max(&block)
flag = true # 1st element? flag = true # 1st element?
result = nil result = nil
self.each{|val| self.each{|*val|
val = val.__svalue
if flag if flag
# 1st element # 1st element
result = val result = val
...@@ -270,7 +272,8 @@ module Enumerable ...@@ -270,7 +272,8 @@ module Enumerable
def min(&block) def min(&block)
flag = true # 1st element? flag = true # 1st element?
result = nil result = nil
self.each{|val| self.each{|*val|
val = val.__svalue
if flag if flag
# 1st element # 1st element
result = val result = val
...@@ -305,11 +308,11 @@ module Enumerable ...@@ -305,11 +308,11 @@ module Enumerable
def partition(&block) def partition(&block)
ary_T = [] ary_T = []
ary_F = [] ary_F = []
self.each{|val| self.each{|*val|
if block.call(val) if block.call(*val)
ary_T.push(val) ary_T.push(val.__svalue)
else else
ary_F.push(val) ary_F.push(val.__svalue)
end end
} }
[ary_T, ary_F] [ary_T, ary_F]
...@@ -324,8 +327,8 @@ module Enumerable ...@@ -324,8 +327,8 @@ module Enumerable
# ISO 15.3.2.2.17 # ISO 15.3.2.2.17
def reject(&block) def reject(&block)
ary = [] ary = []
self.each{|val| self.each{|*val|
ary.push(val) unless block.call(val) ary.push(val.__svalue) unless block.call(*val)
} }
ary ary
end end
...@@ -386,7 +389,7 @@ module Enumerable ...@@ -386,7 +389,7 @@ module Enumerable
# ISO 15.3.2.2.19 # ISO 15.3.2.2.19
def sort(&block) def sort(&block)
ary = [] ary = []
self.each{|val| ary.push(val)} self.each{|*val| ary.push(val.__svalue)}
unless ary.empty? unless ary.empty?
__sort_sub__(ary, ::Array.new(ary.size), 0, 0, ary.size - 1, &block) __sort_sub__(ary, ::Array.new(ary.size), 0, 0, ary.size - 1, &block)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment