How do I iterate over an array of hashes and return the values in a single string?

前端 未结 2 1019
一个人的身影
一个人的身影 2020-12-28 22:18

Sorry if this obvious, I\'m just not getting it. If I have an array of hashes like:

people = [{:name => \"Bob\", :occupation=> \"Builder\"}, {:name =&         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-28 22:51

    Will work too:

    people.each do |person|
      person.each do |key,value|
        print key == :name ? "#{value} : " : "#{value}\n"
      end
    end
    

    Output:

    Bob : Builder
    Jim : Coder
    

提交回复
热议问题