From: Stefan Schmiedl Date: 2002-06-02T17:09:07+09:00 Subject: Re: Problem: Newbie with a question on file arrays and file insertion ... On Sun, 02 Jun 2002 07:36:57 GMT, Kurt Euler wrote: > Ruby pros: > > I am a Ruby newbie, and would appreciate help with the following to jump-start a project. > > 1)- How can I read the contents of a text file into a two-dimentional array, such that one dimension are the rows of text and the other dimension are fields that occur on each line as delimited by some character. > > Note: A tutorial I'm using shows that I can use the readlines method > to create a one-dimensional array, with each line from the source file > an element. It seems like I ought to be able to use the same or > similar method to include the second dimension (delimited fields in > each row). File("whatever").readlines.collect { |line| line.split "separator" } comes to mind here. > > 2)- Say I have text files FileA and FileB. FileA contains a text tag in the somewhere in it called, say, "". How can I: > > - locate the tag in FileA and > - replace with the entire contents of FileB. > > In short, I want to insert FileB in FileA where the tag is, > then delete the tag. if the files are not too large, you could read them as a whole and then just do string substitution. a = File("a").read b = File("b").read a.gsub(//, b) Be warned: this is written on a sunday morning before breakfast :-) s.