Force file write in dos format

Is there any built-in way to force File.puts to use dos line endings,
regardless of the platform under which the program is running?

martin

Use IO#print instead of IO#puts and set $, the default output record
separator:

--------------------------------------------------------------- IO#print
ios.print() => nil
ios.print(obj, …) => nil

 Writes the given object(s) to _ios_. The stream must be opened for
 writing. If the output record separator (+$\+) is not +nil+, it
 will be appended to the output. If no arguments are given, prints
 +$_+. Objects that aren't strings will be converted by calling
 their +to_s+ method. With no argument, prints the contents of the
 variable +$_+. Returns +nil+.

    $stdout.print("This is ", 100, " percent.\n")

 _produces:_

    This is 100 percent.

irb(main):001:0> $\ = “\nanother line\n”
=> “\nanother line\n”
irb(main):002:0> print “test”
test
another line
=> nil
irb(main):003:0>

HTH,

Felix

On 9/1/07, Felix W. [email protected] wrote:

Use IO#print instead of IO#puts and set $, the default output record
separator:

Thanks!

martin

On Sep 1, 2007, at 1:16 PM, Martin DeMello wrote:

Is there any built-in way to force File.puts to use dos line endings,
regardless of the platform under which the program is running?

If I undertand correctly what you want your program is going to
explicitly output pairs “\r\n”.

If that’s the case, in addition to the recommendation about IO#print
already given take into account that the io needs binmode. Otherwise,
on CRLF platforms you’d end up with “\r\r\n” on disk, which would be
wrong. That’s because the single “\n” in “\r\n” gives itself a pair
“\r\n” in text mode. So IO#print CRLF in binmode is the complete
portable solution.

– fxn

On 9/2/07, Xavier N. [email protected] wrote:

on CRLF platforms you’d end up with “\r\r\n” on disk, which would be
wrong. That’s because the single “\n” in “\r\n” gives itself a pair
“\r\n” in text mode. So IO#print CRLF in binmode is the complete
portable solution.

Good point. As of now this will only ever be run on a linux box, but
it certainly doesn’t hurt to future-proof it.

martin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs