Setting the end of line character for puts

I have an array of records that I would like to print.

Using an array, I used only for writing:

puts arr

Then I needed to use the DOS format end-of-line: \ r \ n, so I wrote:

arr.each { |e| print "#{e}\r\n" }

This works correctly, but I would like to know if there is a way to indicate which line-ending format to use so that I can write something like:

$eol = "\r\n"
puts arr

UPDATE 1

I know that puts will use the correct ending strings depending on the platform on which it is running, but I need it because I will write the output to a file.

UPDATE 2 As Mark suggested, setting $ \ is useful. Anyway, this just works for printing. For instance,

irb(main):001:0> a = [1, 2, 3]
=> [1, 2, 3]
irb(main):002:0> $\ = "\r\n"
=> "\r\n"
irb(main):003:0> print a
123
=> nil
irb(main):004:0> puts a
1
2
3
=> nil

print , $\, , puts: $\ .
, # ?

+3
1

Ruby $\ :

>> $\ = '!!!'
=> "!!!"
>> print 'hi'
hi!!!=> nil

$\ $OUTPUT_RECORD_SEPARATOR, English.


Kernel#puts STDOUT.puts; IO.puts " , ". , puts . $, , suck Kernel#print Array#join. , print arr.join, , :

>> [1,2,3].join
=> "123"
>> $, = '---'
=> "---"
>> [1,2,3].join
=> "1---2---3"
>> $\ = '!!!'
=> "!!!"
>> print [1,2,3].join
1---2---3!!!=> nil
+6

Source: https://habr.com/ru/post/1745949/


All Articles