diff --git a/hash.c b/hash.c index 6fa85e81bb..6675b81e65 100644 --- a/hash.c +++ b/hash.c @@ -1503,11 +1503,16 @@ rb_hash_foreach(VALUE hash, rb_foreach_func *func, VALUE farg) if (RHASH_TABLE_EMPTY_P(hash)) return; - hash_iter_lev_inc(hash); arg.hash = hash; arg.func = (rb_foreach_func *)func; arg.arg = farg; - rb_ensure(hash_foreach_call, (VALUE)&arg, hash_foreach_ensure, hash); + if (RB_OBJ_FROZEN(hash)) { + hash_foreach_call((VALUE)&arg); + } + else { + hash_iter_lev_inc(hash); + rb_ensure(hash_foreach_call, (VALUE)&arg, hash_foreach_ensure, hash); + } hash_verify(hash); }