1

I am trying to get data from PostgreSQL with ruby and store them into an array, for that I have a small script:

Script :

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(",")  

The outcome is :

{"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                                     "}

What I need is just the name of each domain name! Something like {"youtube.com","google.com","java.com","rails.com","hp.com"}

Any suggestions would be greatly appreciated.

2
  • 2
    The result you expect ({"youtube.com","google.com",...}) cannot exist in Rails, it looks like a Hash but with only keys (or values?). Did you mean something like an array of the dns names? or an array of hashes containing :dns_name => string_of_dns_name ? Commented Apr 10, 2014 at 13:20
  • something like an array of the dns names ! Commented Apr 10, 2014 at 13:28

2 Answers 2

3

I think you need :

res.flat_map(&:values)
# or if you have hashes having size more then 1, then use the below
res.map { |h| h["dns_name"] }
# to strip leading and trailing whit spaces
res.map { |h| h["dns_name"].strip }
Sign up to request clarification or add additional context in comments.

3 Comments

@midobAck Please be more specific, what doesn't work?
I tried all your suggestions but I get the same outcome as before !
@midobAck Got your point.. do res.map! { |h| h["dns_name"].strip }
0

Taking your code only, and doing small changes, I guess following will work for you

a_dns = []  

res.each { |r| a_dns << r.values[0].strip }  
puts a_dns

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.