From: Samuel Sternhagen on
I have used Net::HTTP like this:

Net::HTTP.get 'google.com', '/index.html'

I would like to pass it a variable instead.

I tried this:

irb(main):001:0> require 'net/http'
=> true
irb(main):002:0> url = String.new
=> ""
irb(main):003:0> url = "'google.com', '/index.html'"
=> "'google.com', '/index.html'"
irb(main):004:0> Net::HTTP.get_print(url)
NoMethodError: undefined method `host' for "'google.com',
'/index.html'":String
from /usr/lib/ruby/1.8/net/http.rb:379:in `get_response'
from /usr/lib/ruby/1.8/net/http.rb:337:in `get_print'
from (irb):4
from :0

How do I pass a variable to Net:HTTP ?
--
Posted via http://www.ruby-forum.com/.

From: tilde on
On 08/09/10 04:18, Samuel Sternhagen wrote:
> url = "'google.com', '/index.html'"

url="google.com/index.html"
Net::HTTP.get_print(URI.parse(url))

get and get_print expect an URI, if you don't specify both host and path
as strings.
From: Jesús Gabriel y Galán on
On Mon, Aug 9, 2010 at 4:18 AM, Samuel Sternhagen <samatoms(a)gmail.com> wrote:
> I have used Net::HTTP like this:
>
> Net::HTTP.get 'google.com', '/index.html'
>
> I would like to pass it a variable instead.

You already got your answer, but I wanted to point out that:

> irb(main):001:0> require 'net/http'
> => true
> irb(main):002:0> url = String.new
> => ""

You don't need to do this, since you are assigning a new value to the
url variable just below. You are creating an object that you don't use
at all.

> irb(main):003:0> url = "'google.com', '/index.html'"
> => "'google.com', '/index.html'"

Jesus.

From: Samuel Sternhagen on
tilde wrote:
> On 08/09/10 04:18, Samuel Sternhagen wrote:
>> url = "'google.com', '/index.html'"
>
> url="google.com/index.html"
> Net::HTTP.get_print(URI.parse(url))
>
> get and get_print expect an URI, if you don't specify both host and path
> as strings.

Thanks :D It is working now.
This one had me stuck for hours.

--
Posted via http://www.ruby-forum.com/.

From: Samuel Sternhagen on
Jesús Gabriel y Galán wrote:
> On Mon, Aug 9, 2010 at 4:18 AM, Samuel Sternhagen <samatoms(a)gmail.com>
> wrote:
>> I have used Net::HTTP like this:
>>
>> Net::HTTP.get 'google.com', '/index.html'
>>
>> I would like to pass it a variable instead.
>
> You already got your answer, but I wanted to point out that:
>
>> irb(main):001:0> require 'net/http'
>> => true
>> irb(main):002:0> url = String.new
>> => ""
>
> You don't need to do this, since you are assigning a new value to the
> url variable just below. You are creating an object that you don't use
> at all.
>
>> irb(main):003:0> url = "'google.com', '/index.html'"
>> => "'google.com', '/index.html'"
>
> Jesus.

Thanks for the tip Jesus. I used to program in C so I have some bad
habits. I just tried my code without creating all those objects and
everything went fine.
--
Posted via http://www.ruby-forum.com/.