diff options
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | enum.c | 6 | ||||
-rw-r--r-- | hash.c | 15 | ||||
-rw-r--r-- | test/ruby/test_enum.rb | 1 |
4 files changed, 18 insertions, 9 deletions
@@ -1,3 +1,8 @@ +Fri Jun 26 07:53:56 2015 Eric Wong <e@80x24.org> + + * enum.c (enum_minmax): simplify return value creation + * test/ruby/test_enum.rb: test behavior on empty + Thu Jun 25 21:24:28 2015 Naohisa Goto <ngotogenome@gmail.com> * test/-ext-/popen_deadlock/test_popen_deadlock.rb: test [Bug #11265] @@ -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 @@ -821,7 +821,7 @@ rb_hash_fetch_m(int argc, VALUE *argv, VALUE hash) if (!RHASH(hash)->ntbl || !st_lookup(RHASH(hash)->ntbl, key, &val)) { if (block_given) return rb_yield(key); if (argc == 1) { - volatile VALUE desc = rb_protect(rb_inspect, key, 0); + VALUE desc = rb_protect(rb_inspect, key, 0); if (NIL_P(desc)) { desc = rb_any_to_s(key); } @@ -3284,7 +3284,7 @@ env_each_pair(VALUE ehash) static VALUE env_reject_bang(VALUE ehash) { - volatile VALUE keys; + VALUE keys; long i; int del = 0; @@ -3301,6 +3301,7 @@ env_reject_bang(VALUE ehash) } } } + RB_GC_GUARD(keys); if (del == 0) return Qnil; return envtbl; } @@ -3370,6 +3371,7 @@ env_select(VALUE ehash) } } } + RB_GC_GUARD(keys); return result; } @@ -3384,7 +3386,7 @@ env_select(VALUE ehash) static VALUE env_select_bang(VALUE ehash) { - volatile VALUE keys; + VALUE keys; long i; int del = 0; @@ -3401,6 +3403,7 @@ env_select_bang(VALUE ehash) } } } + RB_GC_GUARD(keys); if (del == 0) return Qnil; return envtbl; } @@ -3431,7 +3434,7 @@ env_keep_if(VALUE ehash) VALUE rb_env_clear(void) { - volatile VALUE keys; + VALUE keys; long i; keys = env_keys(); @@ -3441,6 +3444,7 @@ rb_env_clear(void) env_delete(Qnil, RARRAY_AREF(keys, i)); } } + RB_GC_GUARD(keys); return envtbl; } @@ -3820,7 +3824,7 @@ env_replace_i(VALUE key, VALUE val, VALUE keys) static VALUE env_replace(VALUE env, VALUE hash) { - volatile VALUE keys; + VALUE keys; long i; keys = env_keys(); @@ -3831,6 +3835,7 @@ env_replace(VALUE env, VALUE hash) for (i=0; i<RARRAY_LEN(keys); i++) { env_delete(env, RARRAY_AREF(keys, i)); } + RB_GC_GUARD(keys); return env; } diff --git a/test/ruby/test_enum.rb b/test/ruby/test_enum.rb index db03b7bb7b..9260c43ec2 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 |