How to import text file and split it up by lines

Hello,

Im attempting to analyze a text file, but having trouble splitting it up
into lines (into an array that indexes each line). Then I would like to
print every line.

Here is what I got:

f = File.open("/Users/john/Desktop/text.txt")

f_lines = f.split("\n")

puts f_lines

And here is the error Im getting:
NoMethodError: private method ‘split’ called for
#<File:/Users/john/Desktop/text.txt>

Dont know why this isnt working. Should be simple enough...

Can someone help please?

···

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

Al Cholic wrote:

Hello,

Im attempting to analyze a text file, but having trouble splitting it up
into lines (into an array that indexes each line). Then I would like to
print every line.

Here is what I got:

f = File.open("/Users/john/Desktop/text.txt")

f_lines = f.split("\n")

puts f_lines

And here is the error Im getting:
NoMethodError: private method ‘split’ called for
#<File:/Users/john/Desktop/text.txt>

Dont know why this isnt working. Should be simple enough...

Can someone help please?

Hi,

you forgot to 'read' file.

f = File.open("/Users/john/Desktop/text.txt")

f_lines = f.read.split("\n")

puts f_lines

Note: you have also 'each_line' in IO, IOString, String, etc.

Vasco Andrade e Silva

···

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

f = File.open("/Users/john/Desktop/text.txt")

f_lines = f.read.split("\n")

There is a convenient shortcut
File.readlines("/User/....")

HTH
Robert

···

On 7/2/07, Vasco Andrade e Silva <vascoas@gmail.com> wrote:
--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck

f_lines = f.read.split("\n")

Thanks man!

···

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