Project

General

Profile

Actions

Bug #21641

closed

Hash#to_s behaves different in 3.4

Bug #21641: Hash#to_s behaves different in 3.4

Added by matheussilvasantos (Matheus Oliveira) 2 days ago. Updated 2 days ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:<unknown>]

Description

I couldn't find anywhere documenting this change from Ruby 3.3 to Ruby 3.4:

3.3.9:

> irb
3.3.9 :001 > {x:1}.to_s
 => "{:x=>1}" 
3.3.9 :002 >

3.4.0:

3.4.0 :001 > {x:1}.to_s
 => "{x: 1}" 
3.4.0 :002 > 

Is there a way to continue with the old behaviour - flag to compilation, monkey patch, etc.?

Unfortunately, the application I'm working on stores the to_s in Redis and use it later; if the result doesn't match for the same Hash, it would break a few things.

Updated by masterleep2 (Bill Lipa) 2 days ago Actions #1

It's mentioned in https://www.ruby-lang.org/en/news/2024/12/25/ruby-3-4-0-released/ under Hash#inspect rendering have been changed. [Bug #20433]

Updated by shan (Shannon Skipper) 2 days ago Actions #2

Please feel free to join us on Ruby Discord or #ruby IRC to discuss all the options! Refining Hash is one way:

module LegacyHashToS
  refine Hash do
    def to_s = "{#{map { "#{_1.inspect}=>#{_2.inspect}" }.join(', ')}}"
  end
end

using LegacyHashToS

{x: 1}.to_s
#=> "{:x=>1}"

Updated by zenspider (Ryan Davis) 2 days ago Actions #3

  • Status changed from Open to Closed

closed by request of OP

Actions

Also available in: PDF Atom