Forum: Ruby Read File Into An Array

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F1bded610e2c4c6e95fcc36114fa8c06?d=identicon&s=25 bc90141 (Guest)
on 2006-05-23 16:28
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.
508bca331a21d64d298561f1079b4113?d=identicon&s=25 Madan Manoharan (Guest)
on 2006-05-23 16:36
(Received via mailing list)
On 5/23/06, bc90141 <info@libertarianswag.com> 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.
F1bded610e2c4c6e95fcc36114fa8c06?d=identicon&s=25 bc90141 (Guest)
on 2006-05-23 16:38
Madan Manoharan wrote:
> On 5/23/06, bc90141 <info@libertarianswag.com> 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.
9358cc96c46055cd68d4a76a9aefe026?d=identicon&s=25 Daniel Harple (Guest)
on 2006-05-23 16:46
(Received via mailing list)
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
62002cee15efcf4628cd7efc19425a07?d=identicon&s=25 Tim Becker (Guest)
on 2006-05-23 16:53
(Received via mailing list)
> 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
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-05-23 17:05
(Received via mailing list)
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
F1bded610e2c4c6e95fcc36114fa8c06?d=identicon&s=25 bc90141 (Guest)
on 2006-05-23 17:27
Tim Becker wrote:

>   contentsArray=[]  # start with an empty array
>   f.each_line {|line|
>     contentArray.push line
>   }
>
> That should do it.
>    -tim

That did it - thanks!  :-)
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2006-05-23 17:46
(Received via mailing list)
2006/5/23, bc90141 <info@libertarianswag.com>:
> That did it - thanks!  :-)
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
9358cc96c46055cd68d4a76a9aefe026?d=identicon&s=25 Daniel Harple (Guest)
on 2006-05-23 17:59
(Received via mailing list)
On May 23, 2006, at 5:43 PM, Robert Klemme 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!  :-)

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

or:

contents_array = IO.readlines("somefile")

Which looks better?

-- Daniel
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2006-05-23 18:09
(Received via mailing list)
2006/5/23, Daniel Harple <dharple@generalconsumption.org>:
> 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
62002cee15efcf4628cd7efc19425a07?d=identicon&s=25 Tim Becker (Guest)
on 2006-05-23 18:09
(Received via mailing list)
> 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
This topic is locked and can not be replied to.