2010/3/17 Shiny Hydra <slotriof / guerrillamailblock.com>:

> I'm new to Ruby and after trying to look through a ton of classes and
> methods, I decided it would be best to ask some more seasoned
> individuals for help. =A0I'm currently working on a project that
> essentially deals with a relational DB in text format. =A0There is a
> standard layout throughout a variety of text files and each of them have
> corresponding information. =A0For example, if positions 10-17 is populate=
d
> with genderM in a file with .aaa extension, male should be written in
> positions 30-34 in a file with .bbb extensions. =A0There can be multiple
> lines, each relating to a different object.

So your file has fixed width records?  This is important to know,
otherwise approach 2 from below becomes tricky (you basically need to
read line by line in order to find the proper position whereas you
otherwise can calculate the position via record size).

> After looking through the file and directory classes I can't find a
> obvious way to code this. =A0How would I write/overwrite a specific
> position number in a certain extension based on information from another
> file? =A0I know to read the information I just use readlines and store th=
e
> position using something similar to textfile1[10,7] and that I can use
> file.extname to get the extension, but beyond this I'm stuck. =A0I
> apologize for the basic question, but I would greatly appreciate the
> help.

You have basically two options:

1. do it in memory

2. do it on disk

ad 1: You can read a complete file by using IO#read into a String,
then you manipulate it via String manipulation methods and write it
out.  This obviously only works up to a certain file size.

ad 2: Use IO#seek to find the proper write position and use IO#write
to overwrite bytes at this position.

Btw, is there a particular reason why you create what looks like a
relational database based on text files?

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/