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.
>
> --
> Posted via http://www.ruby-forum.com/.

Perhaps there are some control characters at the lines' ends.

The way that you're writing to a file seems roundabout
and peculiar to me.

def trim(state)
  open(state + "-cleaned.txt", "w") do |out|
    IO.foreach(state + ".txt") do |line|
      # The next line will show control characters.
      p line
      out.puts line.strip
    end
  end
end