Issue #22209 has been reported by andrykonchin (Andrew Konchin). ---------------------------------------- Bug #22209: IO#set_encoding is ignoring the :newline keyword argument when given Encoding positional argument https://bugs.ruby-lang.org/issues/22209 * Author: andrykonchin (Andrew Konchin) * Status: Open * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- ### 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 ``` -- https://bugs.ruby-lang.org/