我試圖用Ruby和PostgreSQL中獲取數據,並將它們存儲到一個數組格式化數據陣列,對於我有一個小腳本:紅寶石
腳本:
require 'pg'
conn=PGconn.connect(:hostaddr=>"192.168.111.136", :port=>5432, :dbname=>"sentinel_poc",:user=>"sdn_mehdi", :password=>'skyline123')
res = conn.exec("SELECT dns_name FROM dns_table group by dns_name").to_a
a_dns = []
res.each { |r| a_dns << r }
puts a_dns.join(",")
的結果是:
{"dns_name"=>"youtube.com "},{"dns_name"=>"stackoverflow.com "},{"dns_name"=>"cisco.com "},{"dns_name"=>"facebook.com "},{"dns_name"=>"yahoo.com "},{"dns_name"=>"onf.com "},{"dns_name"=>"safe.com "},{"dns_name"=>"linkedin.com "},{"dns_name"=>"amazon.com "},{"dns_name"=>"google.com "},{"dns_name"=>"java.com "},{"dns_name"=>"live.com "},{"dns_name"=>"rails.com "},{"dns_name"=>"postgresql.com "},{"dns_name"=>"hp.com "},{"dns_name"=>"wikipedia.org "}
我需要的只是每個域名的名字!類似{"youtube.com","google.com","java.com","rails.com","hp.com"}
任何建議將不勝感激。
工作,你所期望的結果('{「youtube.com 「,」google.com「,...}')不能存在於Rails中,它看起來像一個哈希,但只有鍵(或值?)。你的意思是像一組DNS名稱的東西?或者包含':dns_name => string_of_dns_name'的散列數組? – MrYoshiji
類似於dns名稱的數組! – midobAck