Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Enumerable#max_by with nil
Browse files Browse the repository at this point in the history
kachick authored and kares committed Dec 14, 2015
1 parent 1711278 commit 25a3876
Showing 2 changed files with 1 addition and 1 deletion.
1 change: 1 addition & 0 deletions core/src/main/java/org/jruby/RubyEnumerable.java
Original file line number Diff line number Diff line change
@@ -1201,6 +1201,7 @@ public static IRubyObject max_by(ThreadContext context, IRubyObject self, final

@JRubyMethod
public static IRubyObject max_by(ThreadContext context, IRubyObject self, IRubyObject arg, final Block block) {
if (arg == context.nil) return singleExtentBy(context, self, "max", SORT_MAX, block);
// TODO: Replace with an implementation (quickselect, etc) which requires O(k) memory rather than O(n) memory
RubyArray sorted = (RubyArray)sort_by(context, self, block);
return ((RubyArray) sorted.last(arg)).reverse();
1 change: 0 additions & 1 deletion spec/tags/ruby/core/enumerable/max_by_tags.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
fails:Enumerable#max_by when called with an argument n without a block returns an enumerator
fails:Enumerable#max_by when called with an argument n when n is nil returns the maximum element

0 comments on commit 25a3876

Please sign in to comment.