From: Tim Becker Date: 2006-05-23T23:50:58+09:00 Subject: Re: Read File Into An Array > 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 On 5/23/06, bc90141 wrote: > Madan Manoharan wrote: > > On 5/23/06, bc90141 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. > > -- > Posted via http://www.ruby-forum.com/. > >