Issue #9314 has been updated by krists.ozols (Krists Ozols).


More examples:

>> "A".encode(Encoding::UTF_16.to_s).bytes
=> [254, 255, 0, 65]
>> "A".encode(Encoding::UTF_16BE).bytes
=> [0, 65]
>> "A".encode(Encoding::UTF_16LE).bytes
=> [65, 0]


There is even a side-effect. After using UTF_16BE suddenly UTF_16 works:
$ irb
>> "some_text".encode(Encoding::UTF_16).bytes
TypeError: no implicit conversion of Encoding into String
	from (irb):1:in `encode'
	from (irb):1
	from /Users/krists/.rvm/rubies/ruby-2.1.0/bin/irb:11:in `<main>'
>> "some_text".encode(Encoding::UTF_16BE).bytes
=> [0, 115, 0, 111, 0, 109, 0, 101, 0, 95, 0, 116, 0, 101, 0, 120, 0, 116]
>> "some_text".encode(Encoding::UTF_16).bytes
=> [254, 255, 0, 115, 0, 111, 0, 109, 0, 101, 0, 95, 0, 116, 0, 101, 0, 120, 0, 116]
----------------------------------------
Bug #9314: String#encode method raises TypeError when called with Encoding::UTF_16 
https://bugs.ruby-lang.org/issues/9314#change-43928

Author: krists.ozols (Krists Ozols)
Status: Rejected
Priority: Normal
Assignee: 
Category: 
Target version: 
ruby -v: 2.1
Backport: 1.9.3: UNKNOWN, 2.0.0: UNKNOWN, 2.1: UNKNOWN


It works with encodings like Encoding::UTF_8..

"test_string".encode(Encoding::UTF_8) # works
"test_string".encode(Encoding::UTF_8.to_s) # works
"test_string".encode(Encoding::UTF_16) # does not work. Error: `encode': no implicit conversion of Encoding into String (TypeError) from ...
"test_string".encode(Encoding::UTF_16.to_s) # works

it did not raise any errors in  previous ruby versions.


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