Project

General

Profile

Actions

Bug #22209

open

IO#set_encoding is ignoring the :newline keyword argument when given Encoding positional argument

Bug #22209: IO#set_encoding is ignoring the :newline keyword argument when given Encoding positional argument

Added by andrykonchin (Andrew Konchin) 5 days ago. Updated 5 days ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:126149]

Description

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:

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:

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
Actions

Also available in: PDF Atom