Sort_by changes frequencies to a 2D array so how does a hash each syntax |key,value| work later?


this line converts the hash to an array because of sort_by method
frequencies = frequencies.sort_by {|k,v| v}
why do we use each iteration for a hash syntax here after we reverse the array, why does this work?!:
frequencies.each { |word, frequency| puts word + " " + frequency.to_s }

Replace this line with your code.


It works because each is an iterator that is not confined to only hashes, but may be applied to an array, which is also an iterable object,


thank you for the feedback and I get that each is an iterator but when you use each on an array I had come to believe it should be used as hash.each {|key,value|...} for hashes and array.each {|index_value|....} but in the example, after frequency is changed to an array, the each syntax still has the |key,value| placeholders. Why and how is the 2nd place holder, "value" treated in terms of the array?


ok i found the answer in another post, good luck guys.