Read File Into An Array

Hi,

I want to read a file into an array. I’m using the following code:

contentsArray = Array.new

f = File.open(“filenamehere”) or die “Unable to open file…”
contentsArray = filenamehere.each_line { |line| }

However, it seems to put the whole file into the array as one item. If
I try and use contentsArray.slice! I get:

undefined method ‘slice!’ for #<File:filenamehere> (NoMethodError)

So it sees the contents of the array as a file instead of a list of text
lines as I want.

What am I doing wrong?

Thanks in advance.

On 5/23/06, bc90141 [email protected] wrote:

Hi,

I want to read a file into an array. I’m using the following code:

contentsArray = Array.new

f = File.open(“filenamehere”) or die “Unable to open file…”
contentsArray = filenamehere.each_line { |line| }

try:

contentsArray = f.each_line {|line|}

instead.

Madan M. wrote:

On 5/23/06, bc90141 [email protected] wrote:

Hi,

I want to read a file into an array. I’m using the following code:

contentsArray = Array.new

f = File.open(“filenamehere”) or die “Unable to open file…”
contentsArray = filenamehere.each_line { |line| }

try:

contentsArray = f.each_line {|line|}

instead.

Sorry… I typed that into the forum incorrectly. I actually have
f.each_line, and it still sees it as a file in the array and not the
individual lines.

On May 23, 2006, at 4:28 PM, bc90141 wrote:

undefined method ‘slice!’ for #<File:filenamehere> (NoMethodError)

So it sees the contents of the array as a file instead of a list of
text
lines as I want.

contents = File.readlines(“somefile”)

If the file does not exist Errno::ENOENT will be raised. The “or die”
is not valid ruby. Try it:

true or die # => true
false or die # =>

~> -:2: undefined local variable or method `die’ for main:Object

(NameError)

– Daniel

f = File.open(“filenamehere”) or die “Unable to open file…”
contentsArray = f.each_line { |line| }

The {|line|} thing after each_line is a code block, which is an
important concept in ruby (you’ll want to read up on it…). It’s like
a anonymous function that you pass as a parameter to another function.
The |line| bit is basically the specification of the parameters of the
anonymous function.

What .each_line does is to call the anonymous function once for
each line in the File object. The anonymous function that you defined
does nothing. What you want it to do is push each line of the file
into the array. So try:

contentsArray=[] # start with an empty array
f.each_line {|line|
contentArray.push line
}

That should do it.
-tim

Tim B. wrote:

contentsArray=[] # start with an empty array
f.each_line {|line|
contentArray.push line
}

That should do it.
-tim

That did it - thanks! :slight_smile:

On Tue, 23 May 2006, bc90141 wrote:

I try and use contentsArray.slice! I get:

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

array = IO.readlines pathname

regards.

-a

On May 23, 2006, at 5:43 PM, Robert K. wrote:

Why do you settly with a complicated version? IO.readlines() will
perfectly suit your needs. If you want to do it while iterating
yourself you should at least use the block form of File.open().

or IO::foreach:

IO.foreach(“somefile”) do |line|

end

anonymous ruby-forum user wrote:

That did it - thanks! :slight_smile:

f = open(“somefile”)
contents_array = []
f.each_line { |line| contents_array << line }
f.close

or:

contents_array = IO.readlines(“somefile”)

Which looks better?

– Daniel

2006/5/23, bc90141 [email protected]:

That did it - thanks! :slight_smile:
Why do you settly with a complicated version? IO.readlines() will
perfectly suit your needs. If you want to do it while iterating
yourself you should at least use the block form of File.open().

File.open your_file do |f|
f.each_line …
end

Kind regards

robert

Why do you settly with a complicated version? IO.readlines() will
perfectly suit your needs.

Of course readlines suits his needs, but it doesn’t explain why the
original code wasn’t working. Codeblocks don’t exist in mainstream
programming languages which makes the concept hard to grasp for people
starting ruby. At that stage of learning it’s probably more helpful to
learn why the way you’re trying to it doesn’t work that to hear that
there is a MUCH easier, but completely different way that does work.
On the other hand, I guess it’s helpful to learn more about the
standard library as well…
-tim

2006/5/23, Daniel H. [email protected]:

end

contents_array = IO.readlines(“somefile”)

Which looks better?

I don’t accept your second variant because you didn’t use the block
form. :-))

robert