Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: rubinius/rubinius
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: a7ba16dbc7e9
Choose a base ref
...
head repository: rubinius/rubinius
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 74a7dbd24850
Choose a head ref
  • 3 commits
  • 3 files changed
  • 2 contributors

Commits on Jan 29, 2016

  1. Spec Enumerable#sort_by when #map does not return an array

    The basic example for this is a lazy enumerable.  In this case, #map
    returns another lazy enumerable, and this breaks #sort_by
    asppsa committed Jan 29, 2016
    Copy the full SHA
    efc8f03 View commit details
  2. Copy the full SHA
    341890e View commit details
  3. Merge pull request #3593 from asppsa/master

    Fix Enumerable#sort_by when #map does not return an array
    brixen committed Jan 29, 2016
    Copy the full SHA
    74a7dbd View commit details
Showing with 35 additions and 1 deletion.
  1. +1 −1 kernel/common/enumerable.rb
  2. +29 −0 spec/ruby/core/enumerable/fixtures/classes.rb
  3. +5 −0 spec/ruby/core/enumerable/sort_by_spec.rb
2 changes: 1 addition & 1 deletion kernel/common/enumerable.rb
Original file line number Diff line number Diff line change
@@ -355,7 +355,7 @@ def sort_by
sort_values = map do
element = Rubinius.single_block_arg
SortedElement.new(element, yield(element))
end
end.to_a

# Now sort the tuple according to the sort by value
sort_values.sort!
29 changes: 29 additions & 0 deletions spec/ruby/core/enumerable/fixtures/classes.rb
Original file line number Diff line number Diff line change
@@ -300,4 +300,33 @@ def to_a
super.freeze
end
end

class MapReturnsEnumerable
include Enumerable

class EnumerableMapping
include Enumerable

def initialize(items, block)
@items = items
@block = block
end

def each
@items.each do |i|
yield @block.call(i)
end
end
end

def each
yield 1
yield 2
yield 3
end

def map(&block)
EnumerableMapping.new(self, block)
end
end
end # EnumerableSpecs utility classes
5 changes: 5 additions & 0 deletions spec/ruby/core/enumerable/sort_by_spec.rb
Original file line number Diff line number Diff line change
@@ -27,5 +27,10 @@
multi.sort_by {|e| e.size}.should == [[1, 2], [3, 4, 5], [6, 7, 8, 9]]
end

it "returns an array of elements when a block is supplied and #map returns an enumerable" do
b = EnumerableSpecs::MapReturnsEnumerable.new
b.sort_by{ |x| -x }.should == [3, 2, 1]
end

it_behaves_like :enumerable_enumeratorized_with_origin_size, :sort_by
end