0
Restaurant = {:name=>"McDonalds",
 :location=>"NYC",
 :chefs=>
  [{:name=>"Sunny", :food=>"fries"},
   {:name=>"Brooklyn", :food=>"burgers"},
   {:name=>"Mac", :food=>"burgers"}],
 :waiters=>
  [{:name=>"Jess", :role=>"senior manager"},
   {:name=>"Sam", :role=>"manager"},
   {:name=>"Jack", :role=>"server"},
   {:name=>"Mary", :role=>"server"}]}

你将如何打印出上面数组中的所有值?

4

3 回答 3

1
Restaurant = {:name=>"McDonalds",
 :location=>"NYC",
 :chefs=>
  [{:name=>"Sunny", :food=>"fries"},
   {:name=>"Brooklyn", :food=>"burgers"},
   {:name=>"Mac", :food=>"burgers"}],
 :waiters=>
  [{:name=>"Jess", :role=>"senior manager"},
   {:name=>"Sam", :role=>"manager"},
   {:name=>"Jack", :role=>"server"},
   {:name=>"Mary", :role=>"server"}]}

keys = Restaurant.map{|k,v| k if v.is_a? Array}.compact

keys.each{|i| Restaurant[i].each{|h| p h.keys,h.values}}

输出:

[:name, :food]
["Sunny", "fries"]
[:name, :food]
["Brooklyn", "burgers"]
[:name, :food]
["Mac", "burgers"]
[:name, :role]
["Jess", "senior manager"]
[:name, :role]
["Sam", "manager"]
[:name, :role]
["Jack", "server"]
[:name, :role]
["Mary", "server"]

keys.each{|i| Restaurant[i].each{|h| p h.to_a.flatten}}

输出:

[:name, "Sunny", :food, "fries"]
[:name, "Brooklyn", :food, "burgers"]
[:name, "Mac", :food, "burgers"]
[:name, "Jess", :role, "senior manager"]
[:name, "Sam", :role, "manager"]
[:name, "Jack", :role, "server"]
[:name, "Mary", :role, "server"]

keys.each{|i| Restaurant[i].each{|h| p h.to_a}}

输出

[[:name, "Sunny"], [:food, "fries"]]
[[:name, "Brooklyn"], [:food, "burgers"]]
[[:name, "Mac"], [:food, "burgers"]]
[[:name, "Jess"], [:role, "senior manager"]]
[[:name, "Sam"], [:role, "manager"]]
[[:name, "Jack"], [:role, "server"]]
[[:name, "Mary"], [:role, "server"]]

更新

需要'pp'

hsh = {:name=>"McDonalds",
 :location=>"NYC",
 :chefs=>
  [{:name=>"Sunny", :food=>"fries"},
   {:name=>"Brooklyn", :food=>"burgers"},
   {:name=>"Mac", :food=>"burgers"}],
 :waiters=>
  [{:name=>"Jess", :role=>"senior manager"},
   {:name=>"Sam", :role=>"manager"},
   {:name=>"Jack", :role=>"server"},
   {:name=>"Mary", :role=>"server"}]}

pp hsh.flat_map{|k,v| v.is_a?(Array) ? (v.flat_map(&:values)) : v }

输出

["McDonalds",
 "NYC",
 "Sunny",
 "fries",
 "Brooklyn",
 "burgers",
 "Mac",
 "burgers",
 "Jess",
 "senior manager",
 "Sam",
 "manager",
 "Jack",
 "server",
 "Mary",
 "server"]

puts hsh.flat_map{|k,v| v.is_a?(Array) ? (v.flat_map(&:values)) : v }.join(",")

# => McDonalds,NYC,Sunny,fries,Brooklyn,burgers,Mac,burgers,Jess,senior manager,Sam,manager,Jack,server,Mary,server
于 2013-07-26T12:31:22.740 回答
0
puts(
  Restaurant
  .values.flatten.flat_map{|v| v.kind_of?(Hash) ? v.values : v}
  .join(", ")
)

# => McDonalds, NYC, Sunny, fries, Brooklyn, burgers, Mac, burgers, Jess, senior manager, Sam, manager, Jack, server, Mary, server
于 2013-07-26T12:48:50.800 回答
0

这是一些格式化输出的示例。正如其他人所问的那样,关于您想要什么样的输出存在一个很大的问题。

Restaurant = {:name=>"McDonalds",
 :location=>"NYC",
 :chefs=>
  [{:name=>"Sunny", :food=>"fries"},
   {:name=>"Brooklyn", :food=>"burgers"},
   {:name=>"Mac", :food=>"burgers"}],
 :waiters=>
  [{:name=>"Jess", :role=>"senior manager"},
   {:name=>"Sam", :role=>"manager"},
   {:name=>"Jack", :role=>"server"},
   {:name=>"Mary", :role=>"server"}]}

def print_restaurant( res, level = 0, indent = 3 )
  res.each do |rk, rv|
    if rv.is_a? Array then
      puts "\n" + (" " * indent * level) + "#{rk.to_s}:"
      rv.each { |cv| print_restaurant(cv, level + 1) }
    else
      puts (" " * indent * level) + "#{rk.to_s}: #{rv}"
    end
  end
end

print_restaurant( Restaurant )

name: McDonalds
location: NYC

chefs:
   name: Sunny
   food: fries
   name: Brooklyn
   food: burgers
   name: Mac
   food: burgers

waiters:
   name: Jess
   role: senior manager
   name: Sam
   role: manager
   name: Jack
   role: server
   name: Mary
   role: server
于 2013-07-26T12:53:12.840 回答