From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS8100 96.44.188.0/22 X-Spam-Status: No, score=-1.5 required=3.0 tests=AWL,BAYES_00,RCVD_IN_XBL shortcircuit=no autolearn=no version=3.3.2 X-Original-To: spew@80x24.org Received: from 80x24.org (manning2.torservers.net [96.44.189.101]) by dcvr.yhbt.net (Postfix) with ESMTP id 8F682633807 for ; Wed, 24 Jun 2015 03:37:07 +0000 (UTC) From: Eric Wong To: spew@80x24.org Subject: [PATCH 2/2] enum.c (enum_minmax): simplify return value creation Date: Wed, 24 Jun 2015 03:37:02 +0000 Message-Id: <1435117022-30592-2-git-send-email-e@80x24.org> In-Reply-To: <1435117022-30592-1-git-send-email-e@80x24.org> References: <1435117022-30592-1-git-send-email-e@80x24.org> List-Id: No need to call three functions on success when one will do. This results in less LoC and smaller object code, too: text data bss dec hex filename 33860 0 296 34156 856c gcc/enum.o-before 33852 0 296 34148 8564 gcc/enum.o --- enum.c | 6 ++---- test/ruby/test_enum.rb | 1 + 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/enum.c b/enum.c index 8a3a463..87ce217 100644 --- a/enum.c +++ b/enum.c @@ -1715,7 +1715,6 @@ enum_minmax(VALUE obj) { struct MEMO *memo = MEMO_NEW(Qundef, Qundef, Qundef); struct minmax_t *m = (struct minmax_t *)&memo->v1; - VALUE ary = rb_ary_new3(2, Qnil, Qnil); m->min = Qundef; m->last = Qundef; @@ -1730,10 +1729,9 @@ enum_minmax(VALUE obj) minmax_i_update(m->last, m->last, m); } if (m->min != Qundef) { - rb_ary_store(ary, 0, m->min); - rb_ary_store(ary, 1, m->max); + return rb_assoc_new(m->min, m->max); } - return ary; + return rb_assoc_new(Qnil, Qnil); } static VALUE diff --git a/test/ruby/test_enum.rb b/test/ruby/test_enum.rb index db03b7b..9260c43 100644 --- a/test/ruby/test_enum.rb +++ b/test/ruby/test_enum.rb @@ -304,6 +304,7 @@ class TestEnumerable < Test::Unit::TestCase assert_equal([1, 3], [2,3,1].minmax) assert_equal([3, 1], [2,3,1].minmax {|a,b| b <=> a }) assert_equal([1, 3], [2,2,3,3,1,1].minmax) + assert_equal([nil, nil], [].minmax) end def test_min_by -- EW