# Maybe I don't understand the .to\_a method

**URL:** <https://www.ruby-forum.com/t/maybe-i-dont-understand-the-to-a-method/99013>\
**Category:** Ruby\
**Created:** [July 19, 2006, 7:40pm UTC](https://www.ruby-forum.com/t/maybe-i-dont-understand-the-to-a-method/99013 "2006-07-19T19:40:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@johnf](https://www.ruby-forum.com/u/johnf)\
**Post date:** [July 19, 2006, 7:40pm UTC](https://www.ruby-forum.com/t/maybe-i-dont-understand-the-to-a-method/99013/1 "2006-07-19T19:40:29Z")

</div>

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/](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]

---

<div class="post-metadata">

**Author:** ![johnf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@johnf](https://www.ruby-forum.com/u/johnf)\
**Post date:** [July 19, 2006, 7:53pm UTC](https://www.ruby-forum.com/t/maybe-i-dont-understand-the-to-a-method/99013/2 "2006-07-19T19:53:27Z")

</div>

> return “This” and test2[1] to return “is”. Test2[0] returns identical  
> def testget(filename)  
> url = URI.parse(‘[http://vineland.lib.muohio.edu/](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…

---

<div class="post-metadata">

**Author:** ![johnf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@johnf](https://www.ruby-forum.com/u/johnf)\
**Post date:** [July 19, 2006, 8:56pm UTC](https://www.ruby-forum.com/t/maybe-i-dont-understand-the-to-a-method/99013/3 "2006-07-19T20:56:30Z")

</div>

On 7/19/06, Philip H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Here’s the program. I know I’m missing something grotesquely obvious,  
> > require ‘net/http’  
> > test = testget(filename)

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

---

<div class="post-metadata">

**Author:** ![johnf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@johnf](https://www.ruby-forum.com/u/johnf)\
**Post date:** [July 19, 2006, 9:06pm UTC](https://www.ruby-forum.com/t/maybe-i-dont-understand-the-to-a-method/99013/4 "2006-07-19T21:06:16Z")

</div>

On 7/19/06, Tanner B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > url = URI.parse(‘[http://vineland.lib.muohio.edu/](http://vineland.lib.muohio.edu/)’ + filename )  
> > > > puts test  
> > > > on
> 
> Try doing test2 = test.body.split (given the sample code you listed  
> above)

That did it!!! Thanks much, Phillip and Tanner.

jf

---

<div class="post-metadata">

**Author:** ![johnf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@johnf](https://www.ruby-forum.com/u/johnf)\
**Post date:** [July 19, 2006, 9:03pm UTC](https://www.ruby-forum.com/t/maybe-i-dont-understand-the-to-a-method/99013/5 "2006-07-19T21:03:06Z")

</div>

On 7/19/06, John F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > res = Net::HTTP.start(url.host, url.port) {|http| http.request(req) }
> 
> 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)

---

<div class="post-metadata">

**Author:** ![johnf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@johnf](https://www.ruby-forum.com/u/johnf)\
**Post date:** [July 19, 2006, 9:12pm UTC](https://www.ruby-forum.com/t/maybe-i-dont-understand-the-to-a-method/99013/6 "2006-07-19T21:12:57Z")

</div>

On 7/19/06, Adam S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> test = testget(filename).split  
> Aha, so that’s why the cryptic stuff at the beginning of the output…  
> thank  
> you!

jf

---

<div class="post-metadata">

**Author:** ![johnf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@johnf](https://www.ruby-forum.com/u/johnf)\
**Post date:** [July 20, 2006, 5:28am UTC](https://www.ruby-forum.com/t/maybe-i-dont-understand-the-to-a-method/99013/7 "2006-07-20T05:28:34Z")

</div>

John F. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![johnf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/848f3c/32.png) [@johnf](https://www.ruby-forum.com/u/johnf)\
**Post date:** [July 19, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/maybe-i-dont-understand-the-to-a-method/99013/8 "2006-07-19T21:09:27Z")

</div>

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
