Project

General

Profile

Bug #16270

Updated by sawa (Tsuyoshi Sawada) over 5 years ago

The following 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} 
 # The following is p's p output content. We content, we can see that e is a hash element, and is converted into an convert to a array object. 
 # This this is expected behavior maybe. Anyway, a maybe, anyway, hash is the 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 is e's Wired, why this time, e output this time different from with each? 
 "246" 
 "247" 
 "248" 
 ``` 

 The following 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; 
 } 
 ``` 

 The following 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, and don't know why the lack of consistency for above two Hash methods lack consistency. But I method, 
 but, i think it confuses me is a little. little confuse me. 

 Thank you. 


Back