===========================
Enumerator in mruby
fib = Enumerator.new do |y|
a = b = 1
loop do
y << a
a, b = b, a + b
end
end
p fib.take(10) #=> [1,1,2,3,5,8,13,21,34,55]
[10,20,30].each.with_index do |i, index|
p [i,index] #=> [10,0], [20,1], [30,1]
end
- class Enumerator
- class Enumerator::Generator
- class Enumerator::Yielder
- class StopIteration < IndexError
- method Kernel#to_enum
- method Kernel#enum_for
- method Kernel#loop
- method Integral#times
- method Array#each
- method Hash#each
- method Range#each
class Some
def each
# add this line return Enumerator object unless block given.
# arguments first is self, second is this method name symbol.
return Enumerator.new self, :each unless block_given?
# old code is OK as it is.
[1,2,3].each do |i|
yield i
end
end
end
Write in /mruby/build_config.rb
MRuby::Build.new do |conf|
conf.gem :github => 'ksss/mruby-enumerator', :branch => 'master'
end
mgem add mruby-enumerator
and exec in your /mruby.
rake clean
rake
rake test
MIT