When I wanted to make a hash instance’s content be sorted by key’s dictionary order, I found a problem.
1 | h.keys.sort.each do |key| |
Also as I know from the Ruby document, Hash has no sort method defined. Then I made a test:
1 | > h = {'x' =>1, 'b'=>2, 'c'=> 3} |
This is the reason why h is an Array instance instead of a Hash instance but it worked, because I found the code has a line “h = h_old.sort” above which defined “h”. So, “h_old” is a Hash instance but “h” is NOT! However you can still use the “each” method in a Hash way on “h” although it’s an Array instance.
But I still don’t understand why the Ruby document does not have the “sort” method for “Hash” class.