Project

General

Profile

Actions

Misc #19391

open

IO #write/#read behaviour when binmode and encoding are explicitly specified

Added by andrykonchin (Andrew Konchin) about 1 year ago.

Status:
Open
Assignee:
-
[ruby-core:112115]

Description

I have a question about how IO operations are supposed to behave when file is opened in binary mode but encoding is specified too.

Documentation of b option of IO.new method and of IO#binmode method is a bit not synchronised and confusing:

IO.open:

"b"  Binary file mode
     Suppresses EOL <-> CRLF conversion on Windows. And
     sets external encoding to ASCII-8BIT unless explicitly
     specified.

IO#binmode:

Puts ios into binary mode. Once a stream is in binary mode, it cannot be reset to nonbinary mode.

newline conversion disabled

encoding conversion disabled

content is treated as ASCII-8BIT

So unless explicitly specified and encoding conversion disabled are a bit contradictory.

Right now I observe that IO.write still encodes an input string in explicitly specified encoding (with encoding option) even if binary mode is set (with binmode option)

File.open(@filename, "w", encoding: Encoding::UTF_32LE, binmode: true) do |file|
  file.write("hi")
end
File.binread(@filename) # => "h\u0000\u0000\u0000i\u0000\u0000\u0000"

No data to display

Actions

Also available in: Atom PDF

Like0