mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Merge branch 'fix-enumerator-initialize-for-nil-or-false' of https://github.com/shuujii/mruby into shuujii-fix-enumerator-initialize-for-nil-or-false
This commit is contained in:
@@ -109,11 +109,11 @@ class Enumerator
|
||||
#
|
||||
# p fib.take(10) # => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
|
||||
#
|
||||
def initialize(obj=nil, meth=:each, *args, &block)
|
||||
def initialize(obj=NONE, meth=:each, *args, &block)
|
||||
if block
|
||||
obj = Generator.new(&block)
|
||||
else
|
||||
raise ArgumentError unless obj
|
||||
elsif obj == NONE
|
||||
raise ArgumentError, "wrong number of arguments (given 0, expected 1+)"
|
||||
end
|
||||
if @obj and !self.respond_to?(meth)
|
||||
raise NoMethodError, "undefined method #{meth}"
|
||||
@@ -221,13 +221,11 @@ class Enumerator
|
||||
end
|
||||
|
||||
def inspect
|
||||
return "#<#{self.class}: uninitialized>" unless @obj
|
||||
|
||||
if @args && @args.size > 0
|
||||
args = @args.join(", ")
|
||||
"#<#{self.class}: #{@obj}:#{@meth}(#{args})>"
|
||||
"#<#{self.class}: #{@obj.inspect}:#{@meth}(#{args})>"
|
||||
else
|
||||
"#<#{self.class}: #{@obj}:#{@meth}>"
|
||||
"#<#{self.class}: #{@obj.inspect}:#{@meth}>"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ assert 'Enumerator.new' do
|
||||
assert_equal [1,2,3], @obj.to_enum(:foo, 1,2,3).to_a
|
||||
assert_equal [1,2,3], Enumerator.new(@obj, :foo, 1,2,3).to_a
|
||||
assert_equal [1,2,3], Enumerator.new { |y| i = 0; loop { y << (i += 1) } }.take(3)
|
||||
assert_equal [], Enumerator.new(false, :__id__).to_a
|
||||
assert_raise(ArgumentError) { Enumerator.new }
|
||||
assert_raise(ArgumentError) { @obj.to_enum }
|
||||
|
||||
@@ -92,11 +93,13 @@ end
|
||||
|
||||
assert 'Enumerator#inspect' do
|
||||
e = (0..10).each
|
||||
assert_equal("#<Enumerator: 0..10:each>", e.inspect)
|
||||
e = Enumerator.new("FooObject", :foo, 1)
|
||||
assert_equal("#<Enumerator: FooObject:foo(1)>", e.inspect)
|
||||
e = Enumerator.new("FooObject", :foo, 1, 2, 3)
|
||||
assert_equal("#<Enumerator: FooObject:foo(1, 2, 3)>", e.inspect)
|
||||
assert_equal('#<Enumerator: 0..10:each>', e.inspect)
|
||||
e = Enumerator.new('FooObject', :foo, 1)
|
||||
assert_equal('#<Enumerator: "FooObject":foo(1)>', e.inspect)
|
||||
e = Enumerator.new('FooObject', :foo, 1, 2, 3)
|
||||
assert_equal('#<Enumerator: "FooObject":foo(1, 2, 3)>', e.inspect)
|
||||
e = Enumerator.new(nil, :to_s)
|
||||
assert_equal('#<Enumerator: nil:to_s>', e.inspect)
|
||||
end
|
||||
|
||||
assert 'Enumerator#each' do
|
||||
|
||||
Reference in New Issue
Block a user