Ruby print hash key and value [closed]
Depending on the contents of your Hash
, you might need to convert the key
to a string since it might be a symbol.
puts key.to_s + ' : ' + value
Or, what I would suggest doing, use string interpolation:
puts "#{key}:#{value}"
The reason you are getting an error, if key
is indeed not a string, is because it is trying to call the method +
on whatever key
is. If it does not have a +
method, you will get an error.