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


#1



https://www.codecademy.com/en/courses/ruby-beginner-en-693PD/0/6?curriculum_id=5059f8619189a5000201fbcb


this line converts the hash to an array because of sort_by method
frequencies = frequencies.sort_by {|k,v| v}
BUT!!!
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.


#2

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,


#3

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?


#4

ok i found the answer in another post, https://discuss.codecademy.com/t/8-8-help-confirming-i-understand-a-few-things/31161. good luck guys.