Permalink
Browse files

Merge pull request #2305 from kachick/fix-enumerable-chunk

Replace a reserved Symbol :_alone instead of :_singleton for Enumerable#chunk
  • Loading branch information...
2 parents 0483eb4 + 21a7430 commit 93408f47e207b1f5b99c48a7742feccaec40345c @dbussink dbussink committed Apr 23, 2013
Showing with 38 additions and 2 deletions.
  1. +2 −2 kernel/common/enumerable19.rb
  2. +36 −0 spec/ruby/core/enumerable/chunk_spec.rb
@@ -15,10 +15,10 @@ def chunk(initial_state = nil, &original_block)
previous = nil
case key
when nil, :_separator
- when :_singleton
+ when :_alone
yielder.yield [key, [val]]
else
- raise RuntimeError, "symbol beginning with an underscore are reserved"
+ raise RuntimeError, "symbols beginning with an underscore are reserved"
end
else
if previous.nil? || previous == key
@@ -40,5 +40,41 @@
ret[3].last.should == [1]
ret[4].last.should == [9]
end
+
+ it "sets a 2-element Array if the block returned :_alone" do
+ ret = EnumerableSpecs::Numerous.new(5,5,2,3,4,5,7,1,9).chunk {|e| e <= 3 && :_alone }.to_a
+ ret.should == [
+ [false, [5, 5]],
+ [:_alone, [2]],
+ [:_alone, [3]],
+ [false, [4, 5, 7]],
+ [:_alone, [1]],
+ [false, [9]]
+ ]
+ end
+
+ it "rejects 2-element Arrays if the block returned nil" do
+ ret = EnumerableSpecs::Numerous.new(5,5,2,3,4,5,7,1,9).chunk {|e| e <= 3 && nil }.to_a
+ ret.should == [
+ [false, [5, 5]],
+ [false, [4, 5, 7]],
+ [false, [9]]
+ ]
+ end
+
+ it "rejects 2-element Arrays if the block returned :_separator" do
+ ret = EnumerableSpecs::Numerous.new(5,5,2,3,4,5,7,1,9).chunk {|e| e <= 3 && :_separator }.to_a
+ ret.should == [
+ [false, [5, 5]],
+ [false, [4, 5, 7]],
+ [false, [9]]
+ ]
+ end
+
+ it "raises an RuntimeError if the block returned a Symbol that is undefined but reserved format (first character is an underscore)" do
+ lambda {
+ EnumerableSpecs::Numerous.new(5,5,2,3,4,5,7,1,9).chunk {|e| e <= 3 && :_singleton }.to_a
+ }.should raise_error(RuntimeError)
+ end
end
end

0 comments on commit 93408f4

Please sign in to comment.