Skip to content

Encoding.default_external = 'ASCII' with StringIO#string results are different from MRI #6528

@koic

Description

@koic

I found a difference in behavior between MRI and JRuby.

Environment Information

Provide at least:

  • jruby 9.2.14.0 (2.5.7) 2020-12-08 ebe64ba Java HotSpot(TM) 64-Bit Server VM 25.271-b09 on 1.8.0_271-b09 +jit [darwin-x86_64]
  • Darwin atelier.local 19.6.0 Darwin Kernel Version 19.6.0: Thu Oct 29 22:56:45 PDT 2020; root:xnu-6153.141.2.2~1/RELEASE_X86_64 x86_64

Expected Behavior

The following is an example:

# example.rb
require 'stringio'

Encoding.default_external = 'ASCII'

io = StringIO.new
io << '©'

p io.string
p io.string.encoding

This is behavior of MRI 2.5.7 (and MRI 3.0.0) .

% ruby -v
ruby 2.5.7p206 (2019-10-01 revision 67816) [x86_64-darwin19]

% ruby example.rb
"\u00A9"
#<Encoding:UTF-8>
% ruby -v
ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin19]

% ruby example.rb
"\u00A9"
#<Encoding:UTF-8>

Actual Behavior

This is behavior of JRuby 9.2.14.0.

% ruby -v
jruby 9.2.14.0 (2.5.7) 2020-12-08 ebe64bafb9 Java HotSpot(TM) 64-Bit Server VM 25.271-b09 on 1.8.0_271-b09 +jit [darwin-x86_64]

% ruby example.rb
"\xC2\xA9"
#<Encoding:US-ASCII>

I encountered this issue in RuboCop.
rubocop/rubocop#9347

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions