From: vil963@... Date: 2019-10-22T16:13:56+00:00 Subject: [ruby-core:95467] [Ruby master Bug#16270] Strange behavior on Hash's #each and #select method. Issue #16270 has been reported by zw963 (Wei Zheng). ---------------------------------------- Bug #16270: Strange behavior on Hash's #each and #select method. https://bugs.ruby-lang.org/issues/16270 * Author: zw963 (Wei Zheng) * Status: Open * Priority: Normal * Assignee: * Target version: * ruby -v: 2.6.3 * Backport: 2.5: UNKNOWN, 2.6: UNKNOWN ---------------------------------------- Following is some example code: ``` ruby sample_hash = { "246" => { "price" => "8000", "note" => "" }, "247" => { "price" => "8000", "note" => "" }, "248" => { "price" => "8000", "note" => "" } } sample_hash.each {|e| p e} # following is p output content, we can see e is a hash element, and convert to a array object. # this is expected behavior maybe, anyway, hash is same as a nested array. ["246", {"price"=>"8000", "note"=>""}] ["247", {"price"=>"8000", "note"=>""}] ["248", {"price"=>"8000", "note"=>""}] sample_hash.select {|e| p e } # Wired, why this time, e output different with each? "246" "247" "248" ``` Following is source code for **each** ```c static VALUE rb_hash_each_pair(VALUE hash) { RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size); if (rb_block_arity() > 1) rb_hash_foreach(hash, each_pair_i_fast, 0); else rb_hash_foreach(hash, each_pair_i, 0); return hash; } ``` Following is source code for **select** ```c VALUE rb_hash_select(VALUE hash) { VALUE result; RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size); result = rb_hash_new(); if (!RHASH_EMPTY_P(hash)) { rb_hash_foreach(hash, select_i, result); } return result; } ``` I don't understand C well, don't know why lack of consistency for above two Hash method, but, i think it is a little confuse me. Thank you. -- https://bugs.ruby-lang.org/ Unsubscribe: