From: Pedro Fortuny Ayuso Date: 2006-12-11T22:02:23+09:00 Subject: Re: Newbie questions ------=_Part_151478_9587708.1165842139360 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline I would do somethink like: a = File.open("name_of_file") whole_file = a.inject("") {|s,e| s+e} a.close That injects into "" each line of a, thus giving you a long string whole_file. However, if you are doing stuff *line by line* you had better do sth like a = File.open("name_of_file") a.each do |line| YOUR STUFF WITH line end a.close For example, you want to count the number of lines where the word "foo" appears a = File.open("myfile") count = 0 a.each do |line| count += 1 if line =~ /foo/ end a.close count has now that value. Though I am not an expert, so listen to more people. Pedro. On 12/11/06, Dav Jones wrote: > > Hello, I'm quite new to this whole Ruby malarky and I'm having some real > trouble with more intermediate features. > You will probally laugh at how simple this stuff is but I really can't > figure it out. The online manuals are all written with the expert in > mind and trial and error doesn't really work as it does with less > complex stuff. > > What I'm wanting to do is read a text file and then do some stuff to it. > So...: > > 1: How do I use the 'source' command to read the file into somewhere > usable? > I'm wanting to do stuff to each line of the file individually once I > have it. > > 2: How do I apply a regular expression to the lines of the file? > > > The most I can get right now is reading the file in and printing it out. > Its just errors galore trying to figure out anything more.... > > -- > Posted via http://www.ruby-forum.com/. > > -- Pedro Fortuny Ayuso C/Capuchinos 14, 1. 47006 Valladolid. SPAIN http://pfortuny.sdf-eu.org ------=_Part_151478_9587708.1165842139360--