Rio instabilities

I’m having some serious problems with rio (rio.rubyforge.org) within my
Rails app and I was wondering if you lot could offer any advice.

So: I’m writing an app that generates folders full of content. The rough
process of what happens when you run the “Generate” method on a “Site”
object is:

it copies a template directory to a directory named (site.directory)
for each line in site.pages it copies
(site.directory)/template_page.html to
(site.directory)/(pagename).html
it then does a great big contents.gsub through each file in the
directory,
replacing some meta-template tags with content.

Now, it creates the files fine, and copies them all fine… but often
throws
errors saying “you can’t gsub here” and the reason you can’t is because
all
the files are empty.

Sometimes, it writes some of the files correctly and the remainder are
empty; quite often, the whole lot are empty. 0kb.

What’s going on? Is this because Ruby is sending commands to the shell
faster than the shell can keep up? I’ve put various sleep commands in,
but
they don’t make a blind bit of difference. Sometimes the whole thing
works
perfectly; sometimes it fails spectacularly. By and large, Rio seems to
work
fine when I mock these functions up in a Ruby script file, but it’s when
I
work this stuff into the Rails application that it starts falling over.
Any
pointers?

Best,

t.