From: "toy (Ivan Kuchin) via ruby-core" Date: 2025-01-31T17:02:20+00:00 Subject: [ruby-core:120850] [Ruby master Bug#21102] Unexpected encoding when concatenating ASCII string with ASCII compatible string with non ASCII encoding Issue #21102 has been reported by toy (Ivan Kuchin). ---------------------------------------- Bug #21102: Unexpected encoding when concatenating ASCII string with ASCII compatible string with non ASCII encoding https://bugs.ruby-lang.org/issues/21102 * Author: toy (Ivan Kuchin) * Status: Open * Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN ---------------------------------------- The problem was noticed in code that is boiled down to: ```ruby # encoding: UTF-8 str = "something" p str.encoding # => # p [nil, str].join.encoding # => # ``` As `nil.to_s` is an empty string with encoding `ASCII` and `"something"` is `ASCII` compatible string, the result is a string with `ASCII` encoding. Event simpler example is `p (nil.to_s + "something").encoding`. Confusing is that resulting encoding depends on order and on compatibility of string encodings: ```ruby # encoding: UTF-8 str1 = "something" # ASCII compatible str2 = "s�m�th�ng" # not ASCII compatible p (nil.to_s + str1).encoding # => # p (nil.to_s + str2).encoding # => # p (str1 + nil.to_s).encoding # => # p (str2 + nil.to_s).encoding # => # ``` I would expect it to behave akin to summing integers and floats or rationals: ```ruby p 1 + 1.0 # => 2.0 p 1.0 + 1 # => 2.0 p 1 + 1r # => (2/1) p 1r + 1 # => (2/1) ``` So it is at least surprising to me. #18579 is probably the most related, but also #14975 and #20594 -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/