Index: EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-enum-ext/mrblib/enum.rb
===================================================================
--- EcnlProtoTool/trunk/mruby-1.2.0/mrbgems/mruby-enum-ext/mrblib/enum.rb	(revision 321)
+++ EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-enum-ext/mrblib/enum.rb	(revision 331)
@@ -59,11 +59,12 @@
   def take(n)
     raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
-    raise ArgumentError, "attempt to take negative size" if n < 0
-
-    n = n.to_int
-    ary = []
-    self.each do |*val|
-      break if ary.size >= n
+    i = n.to_int
+    raise ArgumentError, "attempt to take negative size" if i < 0
+    ary = []
+    return ary if i == 0
+    self.each do |*val|
       ary << val.__svalue
+      i -= 1
+      break if i == 0
     end
     ary
@@ -124,4 +125,5 @@
       block.call(ary.dup) if ary.size == n
     end
+    nil
   end
 
@@ -154,4 +156,5 @@
     end
     block.call(ary) unless ary.empty?
+    nil
   end
 
@@ -215,19 +218,26 @@
   # If the enumerable is empty, the first form returns <code>nil</code>, and the
   # second form returns an empty array.
-  def first(n=NONE)
-    if n == NONE
+  def first(*args)
+    case args.length
+    when 0
       self.each do |*val|
         return val.__svalue
       end
       return nil
+    when 1
+      n = args[0]
+      raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
+      i = n.to_int
+      raise ArgumentError, "attempt to take negative size" if i < 0
+      ary = []
+      return ary if i == 0
+      self.each do |*val|
+        ary << val.__svalue
+        i -= 1
+        break if i == 0
+      end
+      ary
     else
-      a = []
-      i = 0
-      self.each do |*val|
-        break if n<=i
-        a.push val.__svalue
-        i += 1
-      end
-      a
+      raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 0..1)"
     end
   end
@@ -385,9 +395,9 @@
         first = false
       else
+        val = val.__svalue
         if block
-          max = val.__svalue if block.call(*val, max) > 0
-          min = val.__svalue if block.call(*val, min) < 0
+          max = val if block.call(val, max) > 0
+          min = val if block.call(val, min) < 0
         else
-          val = val.__svalue
           max = val if (val <=> max) > 0
           min = val if (val <=> min) < 0
@@ -574,33 +584,36 @@
   #
 
-  def cycle(n=nil, &block)
-    return to_enum(:cycle, n) if !block && n.nil?
-
-    ary = []
-    if n.nil?
-      self.each do|*val|
-        ary.push val
-        block.call(*val)
-      end
-      loop do
-        ary.each do|e|
-          block.call(*e)
-        end
-      end
+  def cycle(nv = nil, &block)
+    return to_enum(:cycle, nv) unless block
+
+    n = nil
+
+    if nv.nil?
+      n = -1
     else
-      raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int)
-
-      n = n.to_int
-      self.each do|*val|
-        ary.push val
-      end
-      count = 0
-      while count < n
-        ary.each do|e|
-          block.call(*e)
-        end
-        count += 1
-      end
-    end
+      unless nv.respond_to?(:to_int)
+        raise TypeError, "no implicit conversion of #{nv.class} into Integer"
+      end
+      n = nv.to_int
+      unless n.kind_of?(Integer)
+        raise TypeError, "no implicit conversion of #{nv.class} into Integer"
+      end
+      return nil if n <= 0
+    end
+
+    ary = []
+    each do |*i|
+      ary.push(i)
+      yield(*i)
+    end
+    return nil if ary.empty?
+
+    while n < 0 || 0 < (n -= 1)
+      ary.each do |i|
+        yield(*i)
+      end
+    end
+
+    nil
   end
 
Index: EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-enum-ext/test/enum.rb
===================================================================
--- EcnlProtoTool/trunk/mruby-1.2.0/mrbgems/mruby-enum-ext/test/enum.rb	(revision 321)
+++ EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-enum-ext/test/enum.rb	(revision 331)
@@ -26,12 +26,14 @@
 assert("Enumerable#each_cons") do
   a = []
-  (1..5).each_cons(3){|e| a << e}
+  b = (1..5).each_cons(3){|e| a << e}
   assert_equal [[1, 2, 3], [2, 3, 4], [3, 4, 5]], a
+  assert_equal nil, b
 end
 
 assert("Enumerable#each_slice") do
   a = []
-  (1..10).each_slice(3){|e| a << e}
+  b = (1..10).each_slice(3){|e| a << e}
   assert_equal [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]], a
+  assert_equal nil, b
 end
 
@@ -129,4 +131,11 @@
   assert_equal ["a", "b", "c", "a", "b", "c"], a
   assert_raise(TypeError) { ["a", "b", "c"].cycle("a") { |v| a << v } }
+
+  empty = Class.new do
+    include Enumerable
+    def each
+    end
+  end
+  assert_nil empty.new.cycle { break :nope }
 end
 
