From: Craig Demyanovich Date: 2009-02-07T23:21:27+09:00 Subject: Re: cannot remove multiple spaces --0016363b85ca296138046254ddbb Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit See comments below. On Sat, Feb 7, 2009 at 8:31 AM, Tom Cloyd wrote: > I'm baffled by this strange outcome - I cannot reduce multiple spaces from > a text file. This isn't just a regex problem, somehow. I'm failing to grasp > something essential, but don't know what it is. All help appreciated, as > usual! > > Here is a demo of my problem, in which I try two different ways, and both > fail: > > === code === > # h2t.rb > > def main > # conversion table spec > conv = [ > [ '

', 'h1. ' ], [ '

', 'h2. ' ], [ '

', 'h3. ' ], > [ '

', 'h4. ' ], [ '

', 'h5. ' ], [ '
', 'h6. ' ], [ /<\/h\d>/, > '' ], > [ " +", ' ' ]] # <= this last array element should do the trick, but > doesn't The last element means replace occurrences of a space followed by a plus with an empty string. I assume that you were trying to write a regular expression, which would make your last array [/ +/, '']. data = open( 'h2t-in2.txt', 'r' ) { |f| ( f.readlines( data )).to_s } > > conv.each do |i| > data.gsub!( i[0], i[1] ) > end > data.squeeze(' ') # <= putting this here was sheer desperations, but even > THIS fails This does nothing because String#squeeze returns a new string that you don't capture. Instead of using the array above, you could do data = data.squeeze(' ') > open( "h2t-out.txt", "w" ) { |f| f.write( data ) } > > end > > %w(rubygems ruby-debug readline strscan logger fileutils).each{ |lib| > require lib } > > main Hope that helps. Regards, Craig --0016363b85ca296138046254ddbb--