summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authormame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-03-17 12:25:40 (GMT)
committermame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-03-17 12:25:40 (GMT)
commit90ab5beb42a226a92d31a1da5cf9d99d276e05e6 (patch)
treeb5b40e7d768b0770778a950a99db3c74881d09f7 /array.c
parent68a6f2e9e0c6fc2ba09463c848dbbb4ece3e07a1 (diff)
* array.c (rb_ary_max, rb_ary_min): implement a block by itself instead
of delegating Enumerable#max/min. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54151 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'array.c')
-rw-r--r--array.c44
1 files changed, 32 insertions, 12 deletions
diff --git a/array.c b/array.c
index f59b4b9..75dbcd4 100644
--- a/array.c
+++ b/array.c
@@ -4210,14 +4210,24 @@ rb_ary_max(int argc, VALUE *argv, VALUE ary)
rb_scan_args(argc, argv, "01", &num);
- if (!NIL_P(num) || rb_block_given_p())
+ if (!NIL_P(num))
return rb_call_super(argc, argv); /* XXX: should redefine? */
- for (i = 0; i < RARRAY_LEN(ary); i++) {
- v = RARRAY_AREF(ary, i);
- if (result == Qundef || OPTIMIZED_CMP(v, result, cmp_opt) > 0) {
- result = v;
- }
+ if (rb_block_given_p()) {
+ for (i = 0; i < RARRAY_LEN(ary); i++) {
+ v = RARRAY_AREF(ary, i);
+ if (result == Qundef || rb_cmpint(rb_yield_values(2, v, result), v, result) > 0) {
+ result = v;
+ }
+ }
+ }
+ else {
+ for (i = 0; i < RARRAY_LEN(ary); i++) {
+ v = RARRAY_AREF(ary, i);
+ if (result == Qundef || OPTIMIZED_CMP(v, result, cmp_opt) > 0) {
+ result = v;
+ }
+ }
}
if (result == Qundef) return Qnil;
return result;
@@ -4255,14 +4265,24 @@ rb_ary_min(int argc, VALUE *argv, VALUE ary)
rb_scan_args(argc, argv, "01", &num);
- if (!NIL_P(num) || rb_block_given_p())
+ if (!NIL_P(num))
return rb_call_super(argc, argv); /* XXX: should redefine? */
- for (i = 0; i < RARRAY_LEN(ary); i++) {
- v = RARRAY_AREF(ary, i);
- if (result == Qundef || OPTIMIZED_CMP(v, result, cmp_opt) < 0) {
- result = v;
- }
+ if (rb_block_given_p()) {
+ for (i = 0; i < RARRAY_LEN(ary); i++) {
+ v = RARRAY_AREF(ary, i);
+ if (result == Qundef || rb_cmpint(rb_yield_values(2, v, result), v, result) < 0) {
+ result = v;
+ }
+ }
+ }
+ else {
+ for (i = 0; i < RARRAY_LEN(ary); i++) {
+ v = RARRAY_AREF(ary, i);
+ if (result == Qundef || OPTIMIZED_CMP(v, result, cmp_opt) < 0) {
+ result = v;
+ }
+ }
}
if (result == Qundef) return Qnil;
return result;