From: "ufuk (Ufuk Kayserilioglu)" Date: 2022-07-11T20:29:07+00:00 Subject: [ruby-core:109188] [Ruby master Bug#18905] :"@=".inspect is non-evaluatable Issue #18905 has been updated by ufuk (Ufuk Kayserilioglu). I was bitten by the same thing last week and was actually going to open an issue myself. Thanks for raising this @qnighy. The thing that caught me out was that the following discrepancy: ```ruby "4_to_5".to_sym.inspect #=> :"4_to_5" "4_to_5=".to_sym.inspect #=> :4_to_5= ``` I realize that Ruby spends a considerable amount of effort to not print symbols in a format where they could not be roundtripped, and the discprancy between `:"4_to_5"` and `:"4_to_5="` was surprising. I bisected the change to the released versions of Ruby and found that the behaviour changed somewhere between `ruby-2.2.0-preview1` and `ruby-2.2.0-preview2`: ``` $ docker run --rm -e "ALL_RUBY_SINCE=ruby-2.0" rubylang/all-ruby ./all-ruby -e "puts '4_to_5='.to_sym.inspect" ruby-2.0.0-p0 :"4_to_5=" ... ruby-2.2.0-preview1 :"4_to_5=" ruby-2.2.0-preview2 :4_to_5= ... ruby-3.2.0-preview1 :4_to_5= ``` Looking at the commit log, I can see that there is a commit that adds special casing for a trailing `=` but I don't understand the reason behind why the change was made: https://github.com/ruby/ruby/commit/986a893d7a420b8472c06b2e36b71b8bf00e9e21 @nobu I understand that Ruby does not guarantee that `inspect` methods are for `eval` but Ruby seems to consistently quote invalid `Symbol`s in `inspect`, except for when they end with `=`. I think this is not consistent and Ruby might as well not quote `Symbol`s in `inspect` at all, in this case. I think something has to change to keep consistency. ---------------------------------------- Bug #18905: :"@=".inspect is non-evaluatable https://bugs.ruby-lang.org/issues/18905#change-98329 * Author: qnighy (Masaki Hara) * Status: Open * Priority: Normal * ruby -v: ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux] * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN ---------------------------------------- There is an edge case where `Symbol#inspect` returns a non-evaluatable expression: ```ruby :"@=".inspect # => :@= :"[][]=".inspect # => :[][]= :"$$$$=" # => :$$$$= ``` More specifically, the quotations are stripped if the following conditions are met (but the converse does not hold): - It starts with `@`, `@@`, `$` or `[`, - the prefix is not followed by an identifier, and - it ends with `=`. But the rule should only apply to `:$=` and `:[]=`, because (in my understanding) we should generally have `eval(sym.inspect) == sym` for the programmers' convenience. -- https://bugs.ruby-lang.org/ Unsubscribe: