Dave Dave - 1 year ago 42
Ruby Question

How can I get both header and web page content in a single call?

I’m using Rails 4.2.7. I know how to use openURI to get the headers from a URL …

open(url){|f| pp f.meta }

and I know how to get the contents of the URL


So how can I get both headers and contents in one call, preferably storing headers into one variable and contents into another?

Answer Source

You just have to reuse the result of the open call:

f = open(url)
pp f.meta
pp f.read