diff options
Diffstat (limited to 'mrblib')
| -rw-r--r-- | mrblib/array.rb | 30 | ||||
| -rw-r--r-- | mrblib/enum.rb | 22 | ||||
| -rw-r--r-- | mrblib/hash.rb | 12 | ||||
| -rw-r--r-- | mrblib/kernel.rb | 2 | ||||
| -rw-r--r-- | mrblib/numeric.rb | 8 | ||||
| -rw-r--r-- | mrblib/string.rb | 54 |
6 files changed, 37 insertions, 91 deletions
diff --git a/mrblib/array.rb b/mrblib/array.rb index d598efc77..6535d6d83 100644 --- a/mrblib/array.rb +++ b/mrblib/array.rb @@ -10,16 +10,16 @@ class Array # and pass the respective element. # # ISO 15.2.12.5.10 - def each(&block) - return to_enum :each unless block + # def each(&block) + # return to_enum :each unless block - idx = 0 - while idx < length - block.call(self[idx]) - idx += 1 - end - self - end + # idx = 0 + # while idx < length + # block.call(self[idx]) + # idx += 1 + # end + # self + # end ## # Calls the given block for each element of +self+ @@ -83,13 +83,15 @@ class Array self end - def _inspect + def _inspect(recur_list) size = self.size return "[]" if size == 0 + return "[...]" if recur_list[self.object_id] + recur_list[self.object_id] = true ary=[] i=0 while i<size - ary<<self[i].inspect + ary<<self[i]._inspect(recur_list) i+=1 end "["+ary.join(", ")+"]" @@ -99,11 +101,7 @@ class Array # # ISO 15.2.12.5.31 (x) def inspect - begin - self._inspect - rescue SystemStackError - "[...]" - end + self._inspect({}) end # ISO 15.2.12.5.32 (x) alias to_s inspect diff --git a/mrblib/enum.rb b/mrblib/enum.rb index 9bd74e1c4..15687e159 100644 --- a/mrblib/enum.rb +++ b/mrblib/enum.rb @@ -65,22 +65,22 @@ module Enumerable end ## - # Call the given block for each element - # which is yield by +each+. Return - # +ifnone+ if no block value was true. - # Otherwise return the first block value - # which had was true. + # Return the first element for which + # value from the block is true. If no + # object matches, calls +ifnone+ and + # returns its result. Otherwise returns + # +nil+. # # ISO 15.3.2.2.4 def detect(ifnone=nil, &block) - ret = ifnone + return to_enum :detect, ifnone unless block + self.each{|*val| if block.call(*val) - ret = val.__svalue - break + return val.__svalue end } - ret + ifnone.call unless ifnone.nil? end ## @@ -284,6 +284,8 @@ module Enumerable # # ISO 15.3.2.2.16 def partition(&block) + return to_enum :partition unless block + ary_T = [] ary_F = [] self.each{|*val| @@ -304,6 +306,8 @@ module Enumerable # # ISO 15.3.2.2.17 def reject(&block) + return to_enum :reject unless block + ary = [] self.each{|*val| ary.push(val.__svalue) unless block.call(*val) diff --git a/mrblib/hash.rb b/mrblib/hash.rb index 609883ecb..b49e987c7 100644 --- a/mrblib/hash.rb +++ b/mrblib/hash.rb @@ -186,15 +186,17 @@ class Hash end # internal method for Hash inspection - def _inspect + def _inspect(recur_list) return "{}" if self.size == 0 + return "{...}" if recur_list[self.object_id] + recur_list[self.object_id] = true ary=[] keys=self.keys size=keys.size i=0 while i<size k=keys[i] - ary<<(k._inspect + "=>" + self[k]._inspect) + ary<<(k._inspect(recur_list) + "=>" + self[k]._inspect(recur_list)) i+=1 end "{"+ary.join(", ")+"}" @@ -204,11 +206,7 @@ class Hash # # ISO 15.2.13.4.30 (x) def inspect - begin - self._inspect - rescue SystemStackError - "{...}" - end + self._inspect({}) end # ISO 15.2.13.4.31 (x) alias to_s inspect diff --git a/mrblib/kernel.rb b/mrblib/kernel.rb index 4700684b6..7c3ea9420 100644 --- a/mrblib/kernel.rb +++ b/mrblib/kernel.rb @@ -40,7 +40,7 @@ module Kernel end # internal method for inspect - def _inspect + def _inspect(_recur_list) self.inspect end diff --git a/mrblib/numeric.rb b/mrblib/numeric.rb index 5a3c5eb58..5926518d5 100644 --- a/mrblib/numeric.rb +++ b/mrblib/numeric.rb @@ -105,14 +105,14 @@ module Integral return to_enum(:step, num, step) unless block i = __coerce_step_counter(num, step) - if num == nil + if num == self || step.infinite? + block.call(i) if step > 0 && i <= (num||i) || step < 0 && i >= (num||-i) + elsif num == nil while true block.call(i) i += step end - return self - end - if step > 0 + elsif step > 0 while i <= num block.call(i) i += step diff --git a/mrblib/string.rb b/mrblib/string.rb index d72002209..0e7c8dc12 100644 --- a/mrblib/string.rb +++ b/mrblib/string.rb @@ -202,60 +202,6 @@ class String self end - ## - # Modify +self+ by replacing the content of +self+. - # The portion of the string affected is determined using the same criteria as +String#[]+. - def []=(*args) - anum = args.size - if anum == 2 - pos, value = args[0], args[1].__to_str - case pos - when String - posnum = self.index(pos) - if posnum - b = self[0, posnum] - a = self[(posnum + pos.length)..-1] - self.replace([b, value, a].join('')) - else - raise IndexError, "string not matched" - end - when Range - head = pos.begin - tail = pos.end - tail += self.length if tail < 0 - unless pos.exclude_end? - tail += 1 - end - return self[head, tail-head]=value - else - pos = pos.__to_int - pos += self.length if pos < 0 - if pos < 0 || pos > self.length - raise IndexError, "index #{args[0]} out of string" - end - b = self[0, pos] - a = self[pos + 1..-1] - self.replace([b, value, a].join('')) - end - return value - elsif anum == 3 - pos, len, value = args[0].__to_int, args[1].__to_int, args[2].__to_str - pos += self.length if pos < 0 - if pos < 0 || pos > self.length - raise IndexError, "index #{args[0]} out of string" - end - if len < 0 - raise IndexError, "negative length #{len}" - end - b = self[0, pos] - a = self[pos + len..-1] - self.replace([b, value, a].join('')) - return value - else - raise ArgumentError, "wrong number of arguments (#{anum} for 2..3)" - end - end - # those two methods requires Regexp that is optional in mruby ## # ISO 15.2.10.5.3 |
