Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Encoding a string with undefined chars fails with NoMethodError #9009

@kares

Description

@kares

Environment Information

Provide at least:

  • JRuby version (jruby -v) and command line (flags, JRUBY_OPTS, etc)
  • 10.0.1.0 / 10.0.2.0

Other relevant info you may wish to add:

  • Installed or activated gems
  • Application/framework version (e.g. Rails, Sinatra)
  • Environment variables

Expected Behavior

Should raise a UndefinedConversionError which it does unless invalid: argument is provided:

ruby -v -e 'p "1ĀŽ2".encode("windows-1252")'

jruby 9.4.13.0 (3.1.4) 2025-06-10 9938a3461f OpenJDK 64-Bit Server VM 21.0.2+13-58 on 21.0.2+13-58 +jit [x86_64-linux]
Encoding::UndefinedConversionError: "\xC4\x80" to WINDOWS-1252 in conversion from UTF-8 to WINDOWS-1252
  encode at org/jruby/RubyString.java:6698
  <main> at -e:1
jruby 10.0.2.0 (3.4.2) 2025-08-07 cba6031bd0 OpenJDK 64-Bit Server VM 21.0.2+13-58 on 21.0.2+13-58 +indy +jit [x86_64-linux]
Encoding::UndefinedConversionError: "\xC4\x80" to WINDOWS-1252 in conversion from UTF-8 to WINDOWS-1252
  encode at org/jruby/RubyString.java:6840
  <main> at -e:1

ruby -v -e 'p "1ĀŽ2".encode("windows-1252", invalid: :replace, replace: "")'

ruby 3.4.6 (2025-09-16 revision dbd83256b1) +PRISM [x86_64-linux]
-e:1:in 'String#encode': U+0100 to WINDOWS-1252 in conversion from UTF-8 to WINDOWS-1252 (Encoding::UndefinedConversionError)
	from -e:1:in '<main>'

Actual Behavior

ruby -v -e 'p "1ĀŽ2".encode("windows-1252", invalid: :replace, replace: "")'
(JRuby 9.4 treats invalid as undef arg)

jruby 9.4.13.0 (3.1.4) 2025-06-10 9938a3461f OpenJDK 64-Bit Server VM 21.0.2+13-58 on 21.0.2+13-58 +jit [x86_64-linux]
"1\x8E2"

ruby -v -e 'p "1ĀŽ2".encode("windows-1252", invalid: :replace, replace: "")'
(JRuby 10 raises NoMethodError instead of Encoding::UndefinedConversionError)

jruby 10.0.2.0 (3.4.2) 2025-08-07 cba6031bd0 OpenJDK 64-Bit Server VM 21.0.2+13-58 on 21.0.2+13-58 +indy +jit [x86_64-linux]
NoMethodError: undefined method '[]' for nil
  encode at org/jruby/RubyString.java:6845
  <main> at -e:1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions