Sorry if this obvious, I'm just not getting it. If I have an array of hashes like:
people = [{:name => "Bob", :occupation=> "Builder"}, {:name => "Jim", :occupation =>
"Coder"}]
And I want to iterate over the array and output strings like: "Bob: Builder". How would I do it? I understand how to iterate, but I'm still a little lost. Right now, I have:
people.each do |person|
person.each do |k,v|
puts "#{v}"
end
end
My problem is that I don't understand how to return both values, only each value separately. What am I missing?
Thank you for your help.
Here you go:
Or:
In answer to the more general query about accessing the values in elements within the array, you need to know that
people
is an array of hashes. Hashes have akeys
method andvalues
method which return the keys and values respectively. With this in mind, a more general solution might look something like:Will work too:
Output: