From: "naruse (Yui NARUSE) via ruby-core" Date: 2025-02-13T08:56:35+00:00 Subject: [ruby-core:120972] [Ruby master Bug#21102] Unexpected encoding when concatenating ASCII string with ASCII compatible string with non ASCII encoding Issue #21102 has been updated by naruse (Yui NARUSE). Status changed from Open to Rejected This behavior is for the case when a string is used as as buffer. In that case the first string is the buffer, and following strings are inputs. Therefore the encoding of the buffer should be respected as far as it can. If you have a real problem which prevents development, please feedback again. ---------------------------------------- Bug #21102: Unexpected encoding when concatenating ASCII string with ASCII compatible string with non ASCII encoding https://bugs.ruby-lang.org/issues/21102#change-111873 * Author: toy (Ivan Kuchin) * Status: Rejected * 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/