Forum: Ruby file.puts define newline

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
(Guest)
on 2009-02-09 15:45
(Received via mailing list)
Hi,

just would like to know if I can somehow define the newline delimeter
when using file.puts ?
e.g. /n, /r/n

Thanks a lot in advance...
badboy (Guest)
on 2009-02-09 15:55
(Received via mailing list)
removed_email_address@domain.invalid schrieb:
> Hi,
>
> just would like to know if I can somehow define the newline delimeter
> when using file.puts ?
> e.g. /n, /r/n
>
> Thanks a lot in advance...
>
hm...I don't think so.
Can't you use file.print and append a "\r\n" or whatever you need?
(Guest)
on 2009-02-09 16:45
(Received via mailing list)
On 9 Feb., 14:50, badboy <removed_email_address@domain.invalid> wrote:
> removed_email_address@domain.invalid schrieb:> Hi,
>
> > just would like to know if I can somehow define the newline delimeter
> > when using file.puts ?
> > e.g. /n, /r/n
>
> > Thanks a lot in advance...
>
> hm...I don't think so.
> Can't you use file.print and append a "\r\n" or whatever you need?

Sure I can do that, but I thought it would be "cleaner" if I could to
something like:
file = new File....
file.newline_delimiter = '/r/n'
or
file.newline_delimiter = 'dos'
badboy (Guest)
on 2009-02-09 16:49
(Received via mailing list)
removed_email_address@domain.invalid schrieb:
> Sure I can do that, but I thought it would be "cleaner" if I could to
> something like:
> file = new File....
> file.newline_delimiter = '/r/n'
> or
> file.newline_delimiter = 'dos'
>
> --
> Volker
>
ugly... newline_delimiter = '/r/n' wouldn't work as you expected =D
it should be \r\n right?
7stud -. (Guest)
on 2009-02-09 18:26
removed_email_address@domain.invalid wrote:
> On 9 Feb., 14:50, badboy <removed_email_address@domain.invalid> wrote:
>> removed_email_address@domain.invalid schrieb:> Hi,
>>
>> > just would like to know if I can somehow define the newline delimeter
>> > when using file.puts ?
>> > e.g. /n, /r/n
>>
>> > Thanks a lot in advance...
>>
>> hm...I don't think so.
>> Can't you use file.print and append a "\r\n" or whatever you need?
>
> Sure I can do that, but I thought it would be "cleaner" if I could to
> something like:
> file = new File....
> file.newline_delimiter = '/r/n'
> or
> file.newline_delimiter = 'dos'

You are supposed to be able to do this:

$OUTPUT_RECORD_SEPARATOR = "apple"

f = File.open("data.txt", "w")
f.write "hello world"
f.write "goodbye"
f.close()

but the output I get in data.txt is:

hello worldgoodbye
7stud -. (Guest)
on 2009-02-09 18:40
7stud -- wrote:
> You are supposed to be able to do this:
>
> $OUTPUT_RECORD_SEPARATOR = "apple"
>
> f = File.open("data.txt", "w")
> f.write "hello world"
> f.write "goodbye"
> f.close()
>
> but the output I get in data.txt is:
>
> hello worldgoodbye

Ok, I found one combination that allows me to define the output
separator:

1) Use this assignment:

$\ = "apple"

2) And the new output record separator only works with print(), not
write():

#$OUTPUT_FIELD_SEPARATOR = "apple"
$\ = "apple"

x = "hello world"
y = "goodbye"

f = File.open("data.txt", "w")
f.write x
f.write y
f.close  #data.txt => hello worldgoodbye

STDOUT.write x
STDOUT.write y  #terminal => hello worldgoodbye


f = File.open("data.txt", "w")
f.print x
f.print y
f.close   #data.txt => hello worldapplegoodbyeapple

print x
print y  #terminal => hello worldapplegoodbyeapple
7stud -. (Guest)
on 2009-02-09 18:41
7stud -- wrote:
> Ok, I found one combination that allows me to define the output
> separator:
>

Mac OSX 10.4.11
This topic is locked and can not be replied to.