Issue #16422 has been reported by zverok (Victor Shepelev).

----------------------------------------
Bug #16422: IO#set_encoding_by_bom when encoding is already set
https://bugs.ruby-lang.org/issues/16422

* Author: zverok (Victor Shepelev)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: 
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
`IO#set_encoding_by_bom` docs [say](https://ruby-doc.org/core-2.7.0.preview3/IO.html#method-i-set_encoding_by_bom):

> If ios is not binmode or **its encoding has been set already**, an exception will be raised.

In reality this does not happen (or I don't understand the meaning of bold statement above):
```ruby
File.write("tmp/bom.txt", "\u{FEFF}ާѧާ")
ios = File.open("tmp/bom.txt", "rb", encoding: 'Windows-1251')
ios.external_encoding
# => #<Encoding:Windows-1251>
ios.set_encoding_by_bom
# => #<Encoding:UTF-8>
ios.external_encoding
# => #<Encoding:UTF-8>
```
...though [in code](https://github.com/ruby/ruby/blob/master/io.c#L8334), seems that error _should_ be raised.

I wonder if it is an error in docs, bug in code, or just my misunderstanding?..



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>