diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2014-03-14 02:41:22 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2014-03-14 02:41:22 +0900 |
| commit | 113ab607846e63c0348758d3e13017e0ea3a45a3 (patch) | |
| tree | 379aa985de0a4c1a148bc693b2207eaf66d44bc3 /mrblib | |
| parent | 463c5f83c3e5f379d4dd59deb17179915fbaf93b (diff) | |
| download | mruby-113ab607846e63c0348758d3e13017e0ea3a45a3.tar.gz mruby-113ab607846e63c0348758d3e13017e0ea3a45a3.zip | |
mruby-enumerator: move definitions in core_mod.rb to mrblib core
Diffstat (limited to 'mrblib')
| -rw-r--r-- | mrblib/array.rb | 2 | ||||
| -rw-r--r-- | mrblib/enum.rb | 2 | ||||
| -rw-r--r-- | mrblib/hash.rb | 4 | ||||
| -rw-r--r-- | mrblib/kernel.rb | 21 | ||||
| -rw-r--r-- | mrblib/numeric.rb | 8 | ||||
| -rw-r--r-- | mrblib/range.rb | 2 |
6 files changed, 29 insertions, 10 deletions
diff --git a/mrblib/array.rb b/mrblib/array.rb index 1203ea70e..9141146e5 100644 --- a/mrblib/array.rb +++ b/mrblib/array.rb @@ -10,6 +10,8 @@ class Array # # ISO 15.2.12.5.10 def each(&block) + return to_enum :each unless block_given? + idx, length = -1, self.length-1 while idx < length and length <= self.length and length = self.length-1 elm = self[idx += 1] diff --git a/mrblib/enum.rb b/mrblib/enum.rb index e6aa682dd..53f2119b0 100644 --- a/mrblib/enum.rb +++ b/mrblib/enum.rb @@ -78,6 +78,8 @@ module Enumerable # # ISO 15.3.2.2.3 def collect(&block) + return to_enum :collect unless block_given? + ary = [] self.each{|val| ary.push(block.call(val)) diff --git a/mrblib/hash.rb b/mrblib/hash.rb index fae44e6f0..c15f770f7 100644 --- a/mrblib/hash.rb +++ b/mrblib/hash.rb @@ -43,7 +43,9 @@ class Hash # # ISO 15.2.13.4.9 def each(&block) - self.keys.each{|k| block.call([k, self[k]])} + return to_enum :each unless block_given? + + self.keys.each { |k| block.call [k, self[k]] } self end diff --git a/mrblib/kernel.rb b/mrblib/kernel.rb index 0277a1b83..fd4dc04ac 100644 --- a/mrblib/kernel.rb +++ b/mrblib/kernel.rb @@ -18,11 +18,12 @@ module Kernel # Calls the given block repetitively. # # ISO 15.3.1.2.8 - def self.loop #(&block) - while(true) - yield - end - end + # provided by Kernel#loop + # def self.loop #(&block) + # while(true) + # yield + # end + # end # 15.3.1.2.3 def self.eval(s) @@ -38,14 +39,22 @@ module Kernel # Alias for +Kernel.loop+. # # ISO 15.3.1.3.29 - def loop #(&block) + def loop + return to_enum :loop unless block_given? + while(true) yield end + rescue => StopIteration + nil end # 11.4.4 Step c) def !~(y) !(self =~ y) end + + def to_enum(*a) + raise NotImplementedError.new("fiber required for enumerator") + end end diff --git a/mrblib/numeric.rb b/mrblib/numeric.rb index b695e946b..034019e8b 100644 --- a/mrblib/numeric.rb +++ b/mrblib/numeric.rb @@ -67,10 +67,12 @@ module Integral # Calls the given block +self+ times. # # ISO 15.2.8.3.22 - def times(&block) + def times &block + return to_enum :times unless block_given? + i = 0 - while(i < self) - block.call(i) + while i < self + block.call i i += 1 end self diff --git a/mrblib/range.rb b/mrblib/range.rb index d1f97ac87..d587cab45 100644 --- a/mrblib/range.rb +++ b/mrblib/range.rb @@ -10,6 +10,8 @@ class Range # # ISO 15.2.14.4.4 def each(&block) + return to_enum :each unless block_given? + val = self.first unless val.respond_to? :succ raise TypeError, "can't iterate" |
