Hey folks,
I'm attempting to write a little program that uses the Net::HTTP library to
fetch a text file. The text file has lines broken up by newlines, and I
thought I could use the .to_a method to convert a variable into an array,
but when I do it it puts the entire text file into the first marker on the
array, and the rest are nil.
Here's the program. I know I'm missing something grotesquely obvious, and
thanks in advance for beating me with the clue stick.
What I expected was for test to return what it returns, but test2[0] to
return "This" and test2[1] to return "is". Test2[0] returns identical
results to test.
jf
···
---
require 'net/http'
require 'uri'
filename = 'test.txt'
def testget(filename)
url = URI.parse('http://vineland.lib.muohio.edu/' + filename )
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(url.host, url.port) {|http| http.request(req) }
end
test = testget(filename)
test2 = test.to_a
puts test
puts test2[0]
puts test2[1]
I'm attempting to write a little program that uses the Net::HTTP library to
fetch a text file. The text file has lines broken up by newlines, and I
thought I could use the .to_a method to convert a variable into an array,
but when I do it it puts the entire text file into the first marker on the
array, and the rest are nil.
Here's the program. I know I'm missing something grotesquely obvious, and
thanks in advance for beating me with the clue stick.
What I expected was for test to return what it returns, but test2[0] to
return "This" and test2[1] to return "is". Test2[0] returns identical
results to test.
jf
---
require 'net/http'
require 'uri'
filename = 'test.txt'
def testget(filename)
url = URI.parse('http://vineland.lib.muohio.edu/' + filename )
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(url.host, url.port) {|http| http.request(req) }
end
test = testget(filename)
test2 = test.to_a
test2 = test.split(/[\n\r]+/)
puts test
puts test2[0]
puts test2[1]
that would "eat" blank lines though, but gets you closer...
John Fink wrote:
Hey folks,
I'm attempting to write a little program that uses the Net::HTTP library to
fetch a text file. The text file has lines broken up by newlines, and I
thought I could use the .to_a method to convert a variable into an array,
but when I do it it puts the entire text file into the first marker on the
array, and the rest are nil.
Here's the program. I know I'm missing something grotesquely obvious, and
thanks in advance for beating me with the clue stick.
What I expected was for test to return what it returns, but test2[0] to
return "This" and test2[1] to return "is". Test2[0] returns identical
results to test.
You are not mistaken; String#to_a returns an array of lines. But you didn't invoke to_a on a string, you invoked it on a Net::HTTPOK object, which just wraps the object inside an array:
test #=> #<Net::HTTPOK 200 OK readbody=true>
test.to_a #=> [#<Net::HTTPOK 200 OK readbody=true>]
test.body.to_a #=> ["this\n", "is\n", "a\n", "test\n"]
Daniel
Aha, split. I've now tried split on arbitrary lines in irb, and it works on
them. But trying split in my little program here yields the following
error:
testget.rb:13: private method `split' called for #<Net::HTTPOK 200 OK
readbody=true> (NoMethodError)
Googling this hairy error gives me no leads.
jf
···
On 7/19/06, Philip Hallstrom <ruby@philip.pjkh.com> wrote:
> I'm attempting to write a little program that uses the Net::HTTP library
to
> fetch a text file. The text file has lines broken up by newlines, and I
> thought I could use the .to_a method to convert a variable into an
array,
> but when I do it it puts the entire text file into the first marker on
the
> array, and the rest are nil.
>
> Here's the program. I know I'm missing something grotesquely obvious,
and
> thanks in advance for beating me with the clue stick.
>
> What I expected was for test to return what it returns, but test2[0] to
> return "This" and test2[1] to return "is". Test2[0] returns identical
> results to test.
>
> jf
> ---
>
> require 'net/http'
> require 'uri'
>
> filename = 'test.txt'
>
> def testget(filename)
> url = URI.parse('http://vineland.lib.muohio.edu/' + filename )
> req = Net::HTTP::Get.new(url.path)
> res = Net::HTTP.start(url.host, url.port) {|http| http.request(req) }
> end
>
> test = testget(filename)
> test2 = test.to_a
test2 = test.split(/[\n\r]+/)
> puts test
> puts test2[0]
> puts test2[1]
that would "eat" blank lines though, but gets you closer...
Try doing test2 = test.body.split (given the sample code you listed above)
···
On 7/19/06, John Fink <john.fink@gmail.com> wrote:
> > require 'net/http'
> > require 'uri'
> >
> > filename = 'test.txt'
> >
> > def testget(filename)
> > url = URI.parse('http://vineland.lib.muohio.edu/' + filename )
> > req = Net::HTTP::Get.new(url.path)
> > res = Net::HTTP.start(url.host, url.port) {|http| http.request(req) }
> > end
> >
> > test = testget(filename)
> > test2 = test.to_a
>
> test2 = test.split(/[\n\r]+/)
>
> > puts test
> > puts test2[0]
> > puts test2[1]
>
> that would "eat" blank lines though, but gets you closer...
>
Aha, split. I've now tried split on arbitrary lines in irb, and it works
on
them. But trying split in my little program here yields the following
error:
testget.rb:13: private method `split' called for #<Net::HTTPOK 200 OK
readbody=true> (NoMethodError)
Googling this hairy error gives me no leads.
jf
--
===Tanner Burson===
tanner.burson@gmail.com
http://tannerburson.com <---Might even work one day...
Http requests return a two-element array with the response and the data.
try :
def testget filename
#...
response,data = Net::HTTP.start(url.host, url.port) {|http| http.request(req) }
data
···
#
test = testget(filename).split
-Adam
On 7/19/06, John Fink <john.fink@gmail.com> wrote:
On 7/19/06, Philip Hallstrom <ruby@philip.pjkh.com> wrote:
>
> > I'm attempting to write a little program that uses the Net::HTTP library
> to
> > fetch a text file. The text file has lines broken up by newlines, and I
> > thought I could use the .to_a method to convert a variable into an
> array,
> > but when I do it it puts the entire text file into the first marker on
> the
> > array, and the rest are nil.
> >
> > Here's the program. I know I'm missing something grotesquely obvious,
> and
> > thanks in advance for beating me with the clue stick.
> >
> > What I expected was for test to return what it returns, but test2[0] to
> > return "This" and test2[1] to return "is". Test2[0] returns identical
> > results to test.
> >
> > jf
> > ---
> >
> > require 'net/http'
> > require 'uri'
> >
> > filename = 'test.txt'
> >
> > def testget(filename)
> > url = URI.parse('http://vineland.lib.muohio.edu/' + filename )
> > req = Net::HTTP::Get.new(url.path)
> > res = Net::HTTP.start(url.host, url.port) {|http| http.request(req) }
> > end
> >
> > test = testget(filename)
> > test2 = test.to_a
>
> test2 = test.split(/[\n\r]+/)
>
> > puts test
> > puts test2[0]
> > puts test2[1]
>
> that would "eat" blank lines though, but gets you closer...
>
Aha, split. I've now tried split on arbitrary lines in irb, and it works on
them. But trying split in my little program here yields the following
error:
testget.rb:13: private method `split' called for #<Net::HTTPOK 200 OK
readbody=true> (NoMethodError)
Googling this hairy error gives me no leads.
jf
That did it!!! Thanks much, Phillip and Tanner.
jf
···
On 7/19/06, Tanner Burson <tanner.burson@gmail.com> wrote:
On 7/19/06, John Fink <john.fink@gmail.com> wrote:
>
> > > require 'net/http'
> > > require 'uri'
> > >
> > > filename = 'test.txt'
> > >
> > > def testget(filename)
> > > url = URI.parse('http://vineland.lib.muohio.edu/' + filename )
> > > req = Net::HTTP::Get.new(url.path)
> > > res = Net::HTTP.start(url.host, url.port) {|http| http.request(req)
}
> > > end
> > >
> > > test = testget(filename)
> > > test2 = test.to_a
> >
> > test2 = test.split(/[\n\r]+/)
> >
> > > puts test
> > > puts test2[0]
> > > puts test2[1]
> >
> > that would "eat" blank lines though, but gets you closer...
> >
>
> Aha, split. I've now tried split on arbitrary lines in irb, and it
works
> on
> them. But trying split in my little program here yields the following
> error:
>
> testget.rb:13: private method `split' called for #<Net::HTTPOK 200 OK
> readbody=true> (NoMethodError)
>
> Googling this hairy error gives me no leads.
>
> jf
Try doing test2 = test.body.split (given the sample code you listed
above)
Aha, so that's why the cryptic stuff at the beginning of the output... thank
you!
jf
···
On 7/19/06, Adam Shelly <adam.shelly@gmail.com> wrote:
Http requests return a two-element array with the response and the data.
try :
def testget filename
#...
response,data = Net::HTTP.start(url.host, url.port) {|http| http.request(req)
}
data
#
test = testget(filename).split