mruby-enumerator: move definitions in core_mod.rb to mrblib core

parent 463c5f83
##
# 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
......@@ -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]
......
......@@ -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))
......
......@@ -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
......
......@@ -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
......@@ -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
......
......@@ -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"
......
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