2011/11/29 Yukihiro Matsumoto <matz / ruby-lang.org>:
> In message "Re: [ruby-core:41362] Re: Wrong encoding of Symbol"
> =A0 =A0on Mon, 28 Nov 2011 21:41:08 +0900, "NARUSE, Yui" <naruse@airemix.=
jp> writes:
>
> |> Matz wrote :
> |>> a =3D :foo
> |>> p a.encoding =A0 =A0 =A0# =3D> #<Encoding:US-ASCII> =A0(a)
> |
> |A symbol which consists of only US-ASCII characters is US-ASCII.
> |
> |>> b =3D "foo"
> |>> p b.encoding =A0 =A0 =A0# =3D> #<Encoding:UTF-8> =A0 =A0 (b)
> |
> |Of course, it is UTF-8.
>
> Considering the fact that a string which consists of only US-ASCII
> characters is UTF-8, a symbol which consists of only US-ASCII being
> US-ASCII is bit awkward for me.

If a symbol keep its source encoding even if it consists of only ASCII,
the symbol distinguish from US-ASCII one.
It means following code raises NoMethodError because UTF-8 :length
doesn't exist.

# coding: utf-8
"foo".__send__(:length)

> |>> c =3D "#{a}foo"
> |>> p c.encoding =A0 =A0 =A0 =A0 =A0# =3D> #<Encoding:US-ASCII> =A0(c)
> |
> |(str + another)'s encoding will be str's encoding when both str and
> |another consist of only ASCII characters.
> |
> |>> d =3D "foo#{a}"
> |>> p d.encoding =A0 =A0 =A0# =3D> #<Encoding:UTF-8> =A0 =A0 (d)
> |
> |So this is "foo"'s encoding.
>
> I understand the internal. =A0I know why the encoding of the symbol is
> US-ASCII. =A0But having above awkwardness, this issue still makes me
> feel inconsistent. =A0A string which consists of only US-ASCII
> characters should be UTF-8, even when a US-ASCII symbol is embedded at
> the top of the string.

It doesn't depend on symbol. It behaves as strings.

--=20
NARUSE, Yui =A0<naruse / airemix.jp>