Ruby: array to hash

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/env ruby
require 'awesome_print'

url = %x[curl -s -I ya.ru |egrep -v HTTP]
options = []
url.split("\r\n").each do |el|
options << el.split(": ")
end

options = options.to_h

puts "Whole Hash-array: "
ap options

puts "Option by key 'Server' "
ap options["Server"]

output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Whole Hash-array:
{
"Server" => "nginx",
"Date" => "Mon, 19 May 2014 17:47:10 GMT",
"Content-Type" => "text/html; charset=UTF-8",
"Content-Length" => "8159",
"Connection" => "close",
"Cache-Control" => "no-cache,no-store,max-age=0,must-revalidate",
"Expires" => "Mon, 19 May 2014 17:47:10 GMT",
"Last-Modified" => "Mon, 19 May 2014 17:47:10 GMT",
"P3P" => "policyref=\"/w3c/p3p.xml\", CP=\"NON DSP ADM DEV PSD IVDo OUR IND STP PHY PRE NAV UNI\"",
"Set-Cookie" => "yandexuid=8305933271400521630; Expires=Thu, 16-May-2024 17:47:10 GMT; Domain=.ya.ru; Path=/",
"X-Frame-Options" => "DENY",
"X-XRDS-Location" => "http://openid.yandex.ru/server_xrds/"
}
Option by key 'Server'
"nginx"