Encoding.compatible? gives inconsistent results with String vs Encoding arguments
Is this a bug? If I pass two strings that have the same encodings as the Encoding instances I pass directly, I get different results.
ruby-1.9.3-p0 :001 > Encoding.compatible?(Encoding::EUC_JP, Encoding::UTF_8)
ruby-1.9.3-p0 :002 > a = "abc".encode "euc-jp"
ruby-1.9.3-p0 :003 > a.encoding
ruby-1.9.3-p0 :004 > a.valid_encoding?
ruby-1.9.3-p0 :005 > b = "def".encode "utf-8"
ruby-1.9.3-p0 :006 > b.encoding
ruby-1.9.3-p0 :007 > b.valid_encoding?
ruby-1.9.3-p0 :008 > Encoding.compatible?(a, b)
ruby-1.9.3-p0 :009 > a << b
ruby-1.9.3-p0 :010 > a.encoding
ruby-1.9.3-p0 :011 > a.valid_encoding?
If this is not a bug, could someone please explain the rationale behind the behavior.
Updated by naruse (Yui NARUSE) almost 8 years ago
It is intended.
The original meaning of Encoding.compatible?(str1, str2) is the encoding of concatenated string.
In other codes, it is
str = str1.dup.concat str2
str.encoding <- this!
If an object is an encoding object, it is considered as a string which is valid, has the encoding, and not ASCII only.
For example UTF-8, Encoding::UTF_8 is considered as a string like "\u3042".