On Dec 28, 2006, at 1:29 AM, Taylor Strait wrote: > I have files with city names which have one or two trailing > whitespaces: > > Adelanto <- > Agoura Hills <- > Alameda <- > Albany <- > Alhambra <- > Aliso Viejo <- > > My method just iterates and strips! > > def trim(state) > diskfile = File.new(state + "-cleaned.txt", "w") > $stdout = diskfile > > IO.foreach(state + ".txt") do |line| > line.strip! > puts line > end > > diskfile.close > $stdout = STDOUT > end > > The output successfully removes leading whitespace but not trailing > whitespace. What am I doing wrong? I would chop! but the number of > trailing whitespace characters varies and my attempt at a while > loop to > check and chop! was unsuccessful. Strip is working as it should. Your input lines don't end is spaces, but with a line end code. The easy way to do what you want is something like the following: <code> #! /usr/bin/env ruby -w PREFOX = "/Users/mg/Desktop/test" SUFFIX = ".txt" File.open(PREFOX + "-cleaned" + SUFFIX, "w") do |out_file| File.open(PREFOX + SUFFIX) do |in_file| in_file.each { |line| out_file.puts line.chomp.strip } end end </code> Note the use of String#chomp. Also, I'm recommending that you use File.open and that you don't mess with $stdout. File#open automatically takes care of closing the files it opens. Regards, Morton