search

Home  >  Q&A  >  body text

ruby - 求教多重 hash 如何取值

求教如何在如下数据中取PublicPort或Image

[#<Docker::Container:0x00000005195578 @id="8aba2f87cdf5e5d340f30fef8e5df8f58d990eab1c70583d4fef3a8fa83de3f2", @info={"Command"=>"/bin/bash", "Created"=>1427722904, "Image"=>"ddddd:latest", "Names"=>["/clever_pike"], "Ports"=>[{"IP"=>"0.0.0.0", "PrivatePort"=>80, "PublicPort"=>10280, "Type"=>"tcp"}], "Status"=>"Up 10 days", "id"=>"8aba2f87cdf5e5d340f30fef8e5df8f58d990eab1c70583d4fef3a8fa83de3f2"}, @connection=#<Docker::Connection:0x00000004aaadf8 @url="unix:///", @options={:socket=>"/var/run/docker.sock"}>>, #<Docker::Container:0x00000005195488 @id="c95642d254f1b716debbc8f5381b1234aff69c92db87a43e9b6f64216d1471b3", @info={"Command"=>"/bin/bash", "Created"=>1427722902, "Image"=>"bbbbbbb:latest", "Names"=>["/serene_heisenberg"], "Ports"=>[{"IP"=>"0.0.0.0", "PrivatePort"=>80, "PublicPort"=>10180, "Type"=>"tcp"}], "Status"=>"Up 10 days", "id"=>"c95642d254f1b716debbc8f5381b1234aff69c92db87a43e9b6f64216d1471b3"}, @connection=#<Docker::Connection:0x00000004aaadf8 @url="unix:///", @options={:socket=>"/var/run/docker.sock"}>>]

以上直接调用docker-api,d=Docker::Container.all获得,相应源代码如下。

rubydef self.all(opts = {}, conn = Docker.connection)
    hashes = Docker::Util.parse_json(conn.get('/containers/json', opts)) || []
    hashes.map { |hash| new(conn, hash) }
end
怪我咯怪我咯2774 days ago761

reply all(1)I'll reply

  • ringa_lee

    ringa_lee2017-04-22 09:02:38

    I don’t quite understand what you want to say, but the hash value is very simple:

    for example

    a = {}
    b = {}
    b["name"] = "bla"
    b["age"] = "12"
    a{"buddy"} = b
    
    a["buddy"]["name"] # => "bla"
    

    reply
    0
  • Cancelreply