New Ruby Line Separator

How to define a new line separator used by the OS (LF, CR / LF or another) in Ruby?

+4
source share
3 answers

Not sure if there is a direct solution to get an OS-based newline type, but there is a $/ variable that contains an β€œinput delimiter”. By default it will be "\ n". ( Documentation here )

You can detect the OS and then set $/ to the "correct" value.

To detect the OS:

 puts RUBY_PLATFORM # => 'i386-linux' require 'rbconfig' puts Config::CONFIG['target_cpu'] # => 'i386' puts Config::CONFIG['target_os'] # => 'linux' puts Config::CONFIG['host_cpu'] # => 'i686' puts Config::CONFIG['host_os'] # => 'linux-gnu' 

Also remember that when reading files, they could have a combination of different line separators - for example, if the text file was edited on both Windows and Linux. Thus, if you process files, do not depend only on the "OS line separator".

+7
source
 File.open("some_temporary_file", "w") { |file| file.puts } new_line_separator = File.open("some_temporary_file", "rb") { |file| file.read } 
0
source

You do not need. In your case, I think you can just use FileUtils.

-1
source

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


All Articles