[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: maybe I don't understand the .to_a method...

Daniel DeLorme

7/20/2006 3:28:00 AM

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