Assign key and value pair to a string in hash each method : each « Hash « Ruby






Assign key and value pair to a string in hash each method


hash = { "key1" => "val1", "key2" => "val2" }
string = ""
hash.each { |k,v| string << "#{k} is #{v}\n" }
puts string

 








Related examples in the same category

1.Iterating Through Hash Elements
2.Use each to loop through a hash
3.Loop hash with interpolation