From: Robert Klemme Date: 2009-08-19T05:15:13+09:00 Subject: Re: fromdos dos2unix in ruby On 18.08.2009 21:46, Robert Dober wrote: > On Tue, Aug 18, 2009 at 7:19 PM, Xavier Noria wrote: >> 2009/8/18 Robert Dober : >> >>> If performance can be an issue we could use File#each with 10.chr as a seperator >>> >>> in.each 10.chr do | line | >>> out.print line.sub( /\r\n\z/, 10.chr ) >>> end >> Just for the record... in Ruby "\n" == 10.chr in all platforms. I find >> "\n" to be more obvious. >> > I wanted to point out the subtle bug because I thought it useful. But > I hate backslashes and use 10.chr often, this however is not good > practice, because it is unconventional, it is just me ;). > In the infinitesimal hope that 10.chr is useful for some folks anyway. I would let Ruby do the line detection to avoid the issue Robert pointed out. For the record, this is what I'd probably be doing: WIN_LE = "\r\n".freeze File.open ARGV[0] do |in| File.open ARGV[1], "wb" do |out| in.each do |line| line.chomp! out.print line, WIN_LE # or: # out.write(line) # out.write(WIN_LE) end end end In this particular case I would not use File.foreach because then "out" is created even if "in" isn't there. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/