[RUBY] Summary of CR, LF, CRL of open method

I made a mistake about the mode, so I will leave it as a memorandum.

What is the open method?

Roughly speaking A method that opens an external text file and allows you to read and write using the mode.

For example, let's say "example.txt" written as follows in an external text file.

This is Tomosuke.

Next, open the file using the open method in Ruby and write it.

f = File.open("example.txt","a")

s = f.write ("My hobby is playing the guitar.") Add to # example.txt f.close

As a result of executing the above

$ more example.txt

This is Tomosuke. # Text before execution $ ruby example.rb $ more example.txt This is Tomosuke. My hobby is playing the guitar. # Text after execution

It will be.

How to use the open method

.open ("path to text", (mode =) mode))

mode Description
"r" Put the file in read mode and open it(This is the default if not specified)
"w" Put the file in write mode and open it
"a" Open the file in write mode and add the written one to the end
"rt" Read CR, LF, CRLF as LF
"rb" CR, LF, and CRLF are all read as they are.
"wb" LF is written as LF as it is.

For "r, w, a", "r +, w +, a +" will open in literacy mode.

About mode "rt, rb, wb"

What I misunderstood was that I thought rt was one mode, r of rt is read, t is read as CR / LF / CRLF of newline character as LF It means that it was meaningful separately. So, if you put together that "b" is "read the line feed code as it is"

mode Description t b
r Read mode Read CR, LF, CRLF as LF CR, LF, and CRLF are all read as they are.

Will come to say. I think this is easy to remember.

What is a line feed code?

A character that issues a line break command to a sentence on a computer CR, LF, CRLF, the line feed code is different. CR is \ n, LF is\ r \, and CRLF is \ r \ n.

Each behavior

Look at the behavior of rt, r, (none) for three types of code text Does rt read CR, LF, or CRLF as LF? What if there is no t In the first place, find out what happens if nothing is added. The text refers to the following.

$ od -c example_lf.txt
0000000    h   e   l   l   o       w   o   r   l   d   !  \n   h   e   l
0000020    l   o       w   o   r   l   d   !                            
0000031
$ od -c example_cr.txt 
0000000    h   e   l   l   o       w   o   r   l   d   !  \r   h   e   l
0000020    l   o       w   o   r   l   d   !                            
0000031
$ od -c example_crlf.txt
0000000    h   e   l   l   o       w   o   r   l   d   !  \r  \n   h   e
0000020    l   l   o       w   o   r   l   d   !                        
0000032

1. For rt

Source code

puts "----- Line feed code LF -----" File.open("example_lf.txt") do |text| text.each_line do |line| p line end end puts "----- Line feed code CR -----" File.open("example_cr.txt") do |text| text.each_line do |line| p line end end puts "----- Line feed code CRLF -----" File.open("example_crlf.txt") do |text| text.each_line do |line| p line end end

The output result is

$ ruby example_1.rb

----- Line feed code LF----- "hello world!\n" "hello world!" ----- Line feed code CR----- "hello world!\n" "hello world!" ----- Line feed code CRLF----- "hello world!\n" "hello world!"

2. For r

Source code

puts "----- Line feed code LF -----" File.open("example_lf.txt","r") do |text| text.each_line do |line| p line end end puts "----- Line feed code CR -----" File.open("example_cr.txt","r") do |text| text.each_line do |line| p line end end puts "----- Line feed code CRLF -----" File.open("example_crlf.txt","r") do |text| text.each_line do |line| p line end end

Output result

$ ruby example_2.rb

----- Line feed code LF----- "hello world!\n" "hello world!" ----- Line feed code CR----- "hello world!\rhello world!" ----- Line feed code CRLF----- "hello world!\r\n" "hello world!"

3. In case of (none)

Source code

puts "----- Line feed code LF -----" File.open("example_lf.txt") do |text| text.each_line do |line| p line end end puts "----- Line feed code CR -----" File.open("example_cr.txt") do |text| text.each_line do |line| p line end end puts "----- Line feed code CRLF -----" File.open("example_crlf.txt") do |text| text.each_line do |line| p line end end

Output result

$ ruby example_3.rb

----- Line feed code LF----- "hello world!\n" "hello world!" ----- Line feed code CR----- "hello world!\rhello world!" ----- Line feed code CRLF----- "hello world!\r\n" "hello world!"

References

https://www.buildinsider.net/language/rubytips/0019 https://docs.ruby-lang.org/ja/latest/method/Kernel/m/open.html

Recommended Posts

Summary of CR, LF, CRL of open method
Summary of Japan time setting and display method
Summary of file reading method for each Java file format
Summary of strong parameters
Summary of jar files
Summary of information security
Summary of using FragmentArgs
definition of ruby method
[Ruby] Method definition summary
Summary of using DBFlow
Summary of Java support 2018
Summary of values returned by the Spliterator characteristics method #java