Loop hash with interpolation : each « Hash « Ruby






Loop hash with interpolation


transform_results = { 4 => 8, 9 => 9, 10 => 6, 2 => 7, 6 => 5 }
transform_results.each { |x, y| puts "f(#{x})=#{y}: difference #{y-x}" }
# f(9)=9: difference 0
# f(6)=5: difference -1
# f(10)=6: difference -4
# f(4)=8: difference 4
# f(2)=7: difference 5

 








Related examples in the same category

1.Iterating Through Hash Elements
2.Use each to loop through a hash
3.Assign key and value pair to a string in hash each method