Thank you all for your kind help. Very much appreciated!
I’ve basically got my little HTML to textile program working well enough
to use. I really didn’t have much time to do it, and your help saved my
bacon.
Comments below…
Robert K. wrote:
You first need to decide whether you want to do all your
transformations in place (i.e. on the original strings in the Array)
or whether you need a copy of all strings - with or without changes.
I ended up doing them in place. Couldn’t figure a clean way to make
copies, then feed them back into the implicit loop.
It’s not clear what you intend to do with results, but I assume for
the moment that you need copies. In that case you probably should not
use String#gsub! but String#gsub (i.e. the version which leaves the
original untouched).
My current code (“fi” is an input textfile - an array of strings):
fi.collect do |x|
delta.each do |y| # <= this is as I specified before, but much
larger
if nil!=x.gsub!(y[0], y[1]) then # <= this makes no obvious
difference in processing time
x.gsub!(y[0], y[1])
end
end
fileout.write x
end
Few other remarks:
- You do not use the block form of file opening and thus you leave the
file descriptor open which is bad.
Erg. Got me.
I used the first, but wasn’t clear about the last. Thanks.
- a Hash seems more appropriate for delta because it nicely expresses
the key value relationship between search criteria and replacement
string and also prevents accidental duplicates. Downside is that you
loose order if that is important for you.
Yeah, and that downside would be fatal. Order matters. Array it must be.
- Reading the file as single String might be more efficient because in
that case you only need one gsub per replacement expression
Oh. Now that’s slick. Many thanks.
Well, I need to study that. Amazing terse. But, then that’s the miracle
of Ruby, isn’t it.
Almost poetry.
Thanks again!
t.
–
Tom C., MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< [email protected] >> (email)
<< TomCloyd.com >> (website & psychotherapy weblog)
<< sleightmind.wordpress.com >> (mental health issues weblog)
<< directpathdesign.com >> (web site design & consultation)