summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-10-13 16:30:07 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-10-13 16:30:07 +0000
commit450307e38315f81c10d959054c49d8baed522027 (patch)
tree55c15b446552b3bfcd27af72a67c236bf0a43cfd
parent63fa57e9ba6c25718843c8b223968bea7adc21d4 (diff)
* enum.c (nmin_run): max(n) and max_by(n) returns an array in
descending order. [ruby-core:65452] Suggested by David Grayson. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47907 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog6
-rw-r--r--enum.c3
-rw-r--r--test/ruby/test_enum.rb6
-rw-r--r--test/ruby/test_range.rb4
4 files changed, 14 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index faecb02318..aa563bae81 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Tue Oct 14 01:27:54 2014 Tanaka Akira <akr@fsij.org>
+
+ * enum.c (nmin_run): max(n) and max_by(n) returns an array in
+ descending order.
+ [ruby-core:65452] Suggested by David Grayson.
+
Mon Oct 13 20:44:49 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
* common.mk (update-gems): chdir to the target directory and then
diff --git a/enum.c b/enum.c
index 3e2532188d..633fa543e1 100644
--- a/enum.c
+++ b/enum.c
@@ -1284,6 +1284,9 @@ nmin_run(VALUE obj, VALUE num, int by, int rev)
ruby_qsort(RARRAY_PTR(result), RARRAY_LEN(result), sizeof(VALUE),
data.cmpfunc, (void *)&data);
}
+ if (rev) {
+ rb_ary_reverse(result);
+ }
*((VALUE *)&RBASIC(result)->klass) = rb_cArray;
return result;
diff --git a/test/ruby/test_enum.rb b/test/ruby/test_enum.rb
index edd389b071..6448796790 100644
--- a/test/ruby/test_enum.rb
+++ b/test/ruby/test_enum.rb
@@ -245,8 +245,8 @@ class TestEnumerable < Test::Unit::TestCase
assert_equal("horse", ary.max)
assert_equal("albatross", ary.max {|a,b| a.length <=> b.length })
assert_equal(1, [3,2,1].max{|a,b| b <=> a })
- assert_equal(%w[dog horse], ary.max(2))
- assert_equal(%w[horse albatross],
+ assert_equal(%w[horse dog], ary.max(2))
+ assert_equal(%w[albatross horse],
ary.max(2) {|a,b| a.length <=> b.length })
end
@@ -278,7 +278,7 @@ class TestEnumerable < Test::Unit::TestCase
a = %w(albatross dog horse)
assert_equal("albatross", a.max_by {|x| x.length })
assert_equal(1, [2,3,1].max_by {|x| -x })
- assert_equal(%w[horse albatross], a.max_by(2) {|x| x.length })
+ assert_equal(%w[albatross horse], a.max_by(2) {|x| x.length })
end
def test_minmax_by
diff --git a/test/ruby/test_range.rb b/test/ruby/test_range.rb
index 1031201923..e3880bff93 100644
--- a/test/ruby/test_range.rb
+++ b/test/ruby/test_range.rb
@@ -81,8 +81,8 @@ class TestRange < Test::Unit::TestCase
assert_equal(0, (0..0).max)
assert_equal(nil, (0...0).max)
- assert_equal([8,9,10], (0..10).max(3))
- assert_equal([7,8,9], (0...10).max(3))
+ assert_equal([10,9,8], (0..10).max(3))
+ assert_equal([9,8,7], (0...10).max(3))
end
def test_initialize_twice