Issue #7200 has been updated by brixen (Brian Ford).


#set_encoding accepts ("bom|utf-16be:euc-jp") but rejects ("bom|utf-16be", "euc-jp"). This is inconsistent, confusing, and has nothing to do with the artificial mode vs encoding justification above. This inconsistency requires additional code that is subject to bugs.

The fact that there were no tests for this until I wrote the RubySpecs illustrates the inconsistency, confusion, and susceptibility to ad hoc implementation-defined semantics. I still don't see a single test for #set_encoding with "bom|" arguments in the MRI tests. Or am I missing something?

Cheers,
Brian
----------------------------------------
Bug #7200: Setting external encoding with BOM|
https://bugs.ruby-lang.org/issues/7200#change-36676

Author: brixen (Brian Ford)
Status: Rejected
Priority: Normal
Assignee: naruse (Yui NARUSE)
Category: 
Target version: 2.0.0
ruby -v: ruby 1.9.3p286 (2012-10-12 revision 37165) [x86_64-darwin10.8.0]


File.open will accept, for example, :encoding => "bom|utf-16be:euc-jp" or :encoding => "bom|utf-16be". However, :external_encoding => "bom|utf-16be" raises an ArgumentError. Likewise, IO#set_encoding will accept "bom|utf-16be:euc-jp" but raises an ArgumentError if passed "bom|utf-16be", "euc-jp".

It is inconsistent to accept "bom|utf-*" in some cases and not others.

See the following IRB transcript.

$ irb
1.9.3p286 :001 > f = File.open "foo.txt", "r", :encoding => "bom|utf-16be:euc-jp"
 => #<File:foo.txt> 
1.9.3p286 :002 > f.internal_encoding
 => #<Encoding:EUC-JP> 
1.9.3p286 :003 > f.external_encoding
 => #<Encoding:UTF-16BE> 
1.9.3p286 :004 > f.close
 => nil 
1.9.3p286 :005 > f = File.open "foo.txt", "r"
 => #<File:foo.txt> 
1.9.3p286 :006 > f.set_encoding "bom|utf-16be:euc-jp"
 => #<File:foo.txt> 
1.9.3p286 :007 > f.internal_encoding
 => #<Encoding:EUC-JP> 
1.9.3p286 :008 > f.external_encoding
 => #<Encoding:UTF-16BE> 
1.9.3p286 :009 > f.close
 => nil 
1.9.3p286 :010 > f = File.open "foo.txt", "r"
 => #<File:foo.txt> 
1.9.3p286 :011 > f.set_encoding "bom|utf-16be", "euc-jp"
ArgumentError: unknown encoding name - bom|utf-16be
	from (irb):11:in `set_encoding'
	from (irb):11
	from /Users/brian/.rvm/rubies/ruby-1.9.3-p286/bin/irb:16:in `<main>'
1.9.3p286 :012 > f = File.open "foo.txt", "w", :external_encoding => "bom|utf-16be"
ArgumentError: unknown encoding name - bom|utf-16be
	from (irb):12:in `initialize'
	from (irb):12:in `open'
	from (irb):12
	from /Users/brian/.rvm/rubies/ruby-1.9.3-p286/bin/irb:16:in `<main>'
1.9.3p286 :013 > f = File.open "foo.txt", "rb", :encoding => "bom|utf-16be"
 => #<File:foo.txt> 

Thanks,
Brian


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