Skip to content

Commit

Permalink
Showing 2 changed files with 28 additions and 4 deletions.
8 changes: 4 additions & 4 deletions corelib/enumerable.rb
Original file line number Diff line number Diff line change
@@ -78,15 +78,15 @@ def any?(&block)
end

def collect(&block)
return enum_for :collect unless block_given?

%x{
var result = [];
var proc = function() {
var value;
var param = arguments.length == 1 ?
arguments[0] : $slice.call(arguments);
var value, args = $slice.call(arguments);
if ((value = block(param)) === $breaker) {
if ((value = block.apply(null, arguments)) === $breaker) {
return $breaker.$v;
}
24 changes: 24 additions & 0 deletions spec/rubyspec/core/enumerable/collect_spec.rb
Original file line number Diff line number Diff line change
@@ -12,4 +12,28 @@
numerous.collect { |i| i % 2 }.should == [0, 1, 1, 0, 1, 0]
numerous.collect { |i| i }.should == entries
end

ruby_version_is ""..."1.9" do
it "gathers whole arrays as elements when each yields multiple" do
multi = EnumerableSpecs::YieldsMulti.new
multi.send(@method) {|e| e}.should == [[1,2],[3,4,5],[6,7,8,9]]
end

it "returns to_a when no block given" do
EnumerableSpecs::Numerous.new.send(@method).should == [2, 5, 3, 6, 1, 4]
end
end

ruby_version_is "1.9" do
it "gathers initial args as elements when each yields multiple" do
multi = EnumerableSpecs::YieldsMulti.new
multi.collect {|e| e}.should == [1, 3, 6]
end

it "returns an enumerator when no block given" do
enum = EnumerableSpecs::Numerous.new.collect
enum.should be_an_instance_of(enumerator_class)
enum.each { |i| -i }.should == [-2, -5, -3, -6, -1, -4]
end
end
end

0 comments on commit 77c5e20

Please sign in to comment.