From: Robert Klemme Date: 2008-06-16T18:10:43+09:00 Subject: Re: how to stop gsub from returning nil 2008/6/16 Tom Cloyd : > I'm trying to use gsub to do a number of transformations in an array of > strings. I find that when a particular transformation does NOT happen, > because the searched-for substring is not there, gsub returns nil. This > effectively ruins my output. I don't want nothing. I want the string that's > being processed, returned with or without any transformations. Is there any > alternative to testing for a return of nil before calling gsub, so as to > avoid the wiping out of my string? I've looked for something to use other > than String::gsub, and have not found anything. 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. > Code: > > filein = open( "{whatever}" ) > fi = filein.readlines > delta = [ ["

", ''], ["", ''] ] > results = fi.collect do |x| > delta.each do |y| > debugger x.gsub!(y[0], y[1]) > end > end 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). Few other remarks: - You do not use the block form of file opening and thus you leave the file descriptor open which is bad. - You can read a complete file as Array via File.readlines("whatever") - You can read a complete file as String via File.read("whatever") - 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. - Reading the file as single String might be more efficient because in that case you only need one gsub per replacement expression So, here's probably what I'd do delta = { %r{}i => '' } c = File.read "whatever.html" delta.each do |rx, repl| c.gsub! rx, repl end puts c Kind regards robert -- use.inject do |as, often| as.you_can - without end