From: Tom Cloyd Date: 2008-06-16T19:39:15+09:00 Subject: Re: how to stop gsub from returning nil Robert Dober wrote: > On Mon, Jun 16, 2008 at 9:58 AM, Tom Cloyd wrote: > >> 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. >> >> 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 >> >> t. >> >> -- >> >> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >> Tom Cloyd, MS MA, LMHC >> Private practice Psychotherapist >> Bellingham, Washington, U.S.A: (360) 920-1226 >> << tc@tomcloyd.com >> (email) >> << TomCloyd.com >> (website & psychotherapy weblog) >> << sleightmind.wordpress.com >> (mental health issues weblog) >> << directpathdesign.com >> (web site design & consultation) >> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >> >> >> >> > > > > Why not write a simple wrapper? > > Ruby 1.9 > class String > def mysub! *args, &blk > tap{ gsub!( *args, &blk ) } > end > end > > Ruby 1.8 or 1.9 too ;) > ... > def mysub!... > gsub!.... > self > end > > > Cheers > Robert > Simple answer: I don't know the idiom. I have no idea what you just said. None. Wrappers in my world are for candy. (!) I haven't caught up yet with your *last* code, but I'm working on it. Then I'll go to work on THIS. I much appreciate the learning challenges you're sending my way. Hope some others are benefiting as well. Thanks! t. -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tom Cloyd, MS MA, LMHC Private practice Psychotherapist Bellingham, Washington, U.S.A: (360) 920-1226 << tc@tomcloyd.com >> (email) << TomCloyd.com >> (website & psychotherapy weblog) << sleightmind.wordpress.com >> (mental health issues weblog) << directpathdesign.com >> (web site design & consultation) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~