summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2014-03-14 02:41:22 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2014-03-14 02:41:22 +0900
commit113ab607846e63c0348758d3e13017e0ea3a45a3 (patch)
tree379aa985de0a4c1a148bc693b2207eaf66d44bc3
parent463c5f83c3e5f379d4dd59deb17179915fbaf93b (diff)
downloadmruby-113ab607846e63c0348758d3e13017e0ea3a45a3.tar.gz
mruby-113ab607846e63c0348758d3e13017e0ea3a45a3.zip
mruby-enumerator: move definitions in core_mod.rb to mrblib core
-rw-r--r--mrbgems/mruby-enumerator/mrblib/core_mod.rb98
-rw-r--r--mrblib/array.rb2
-rw-r--r--mrblib/enum.rb2
-rw-r--r--mrblib/hash.rb4
-rw-r--r--mrblib/kernel.rb21
-rw-r--r--mrblib/numeric.rb8
-rw-r--r--mrblib/range.rb2
7 files changed, 29 insertions, 108 deletions
diff --git a/mrbgems/mruby-enumerator/mrblib/core_mod.rb b/mrbgems/mruby-enumerator/mrblib/core_mod.rb
deleted file mode 100644
index 968cc6ac4..000000000
--- a/mrbgems/mruby-enumerator/mrblib/core_mod.rb
+++ /dev/null
@@ -1,98 +0,0 @@
-##
-# modifying existing methods
-##
-
-# See /mrblib/kernel.rb
-module Kernel
- def loop
- return to_enum :loop unless block_given?
-
- while(true)
- yield
- end
- rescue => StopIteration
- nil
- end
-end
-
-# See /mrblib/numeric.rb
-module Integral
- def times &block
- return to_enum :times unless block_given?
-
- i = 0
- while i < self
- block.call i
- i += 1
- end
- self
- end
-end
-
-# See /mrblib/enum.rb
-module Enumerable
- def collect(&block)
- return to_enum :collect unless block_given?
-
- ary = []
- self.each{|val|
- ary.push(block.call(val))
- }
- ary
- end
- alias map collect
-end
-
-# See /mrblib/array.rb
-class Array
- 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]
- unless elm
- if elm == nil and length >= self.length
- break
- end
- end
- block.call(elm)
- end
- self
- end
-end
-
-# See /mrblib/hash.rb
-class Hash
- def each(&block)
- return to_enum :each unless block_given?
-
- self.keys.each { |k| block.call [k, self[k]] }
- self
- end
-end
-
-# See /mrblib/range.rb
-class Range
- def each &block
- return to_enum :each unless block_given?
-
- val = self.first
- unless val.respond_to? :succ
- raise TypeError, "can't iterate"
- end
-
- last = self.last
- return self if (val <=> last) > 0
-
- while((val <=> last) < 0)
- block.call(val)
- val = val.succ
- end
-
- if not exclude_end? and (val <=> last) == 0
- block.call(val)
- end
- self
- end
-end
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"