15. more methods, more solutions. each_key and each_value redundant methods?

I can immediately see that these are redundant,and you can iterate over a hash with .each and easily achieve the same effect. I just want to make sure I am not missing anything?

movie_ratings.each do |x,y|
puts x

1 Like

Yes you can, but if you only need the key or only need the value, you don’t need to use each in the first place.

Ruby is nothing if not diverse. It is the panacea of diversity in coding options. All of this is geared to specific goals, admittedly, but the paths we can take to achieving them are many. There is nothing black and white about Ruby that I can see, but then I’m still a newbie.

No doubt if we were to peer under the hood of JavaScript we might find code that mimics an each_key iteration block for,

obj = {
    key1: 1,
    key2: 2,
    key3: 3
//  [ 'key1', 'key2', 'key3' ]

Sans the semi-colon, obj is a spllitting image of a 1.9 rocketless hash.

obj.each_key { |k| puts k }

I doubt that JS could achieve in 1 sec. what Ruby can in 1 ms.

Thank you for giving a hint.
I passed the exam after writing below cord.

movie_ratings.each_key { |k| puts k }

1 Like