Project

General

Profile

Bug #22209

Updated by andrykonchin (Andrew Konchin) about 2 months ago

### Behavioral Discrepancy of the `:newline` Option with Single `Encoding` Objects 

 When calling `IO#set_encoding` with a single positional argument, the `:newline` option (e.g., `newline: :universal`) is parsed and applied only when the encoding is passed as a `String`. When passed as an `Encoding` object, the option is silently ignored and no validation or conversion is performed. 

 #### 1. Passing Encoding as a String (option applied correctly) 

 When the encoding is specified as a `String`, the `:newline` option is parsed and correctly normalizes CRLF line endings to LF: 

 ```ruby 
 data = "line1\r\nline2\r\n" 
 File.write("1.txt", data) 

 File.open("1.txt") do |f| 
   f.set_encoding("utf-8", newline: :universal) 
   p f.read # => "line1\nline2\n" 
 end 
 ``` 


 #### 2. Passing Encoding as an Encoding Object (option ignored) 

 When the encoding is specified as an Encoding object, the `:newline` option is ignored, and the CRLF line endings remain unmodified: 

 ```ruby 
 data = "line1\r\nline2\r\n" 
 File.write("1.txt", data) 

 File.open("1.txt") do |f| 
   f.set_encoding(Encoding::UTF_8, newline: :universal) 
   p f.read # => "line1\r\nline2\r\n" 
 end 
 ``` 

Back