[ruby-core:113285] [Ruby master Bug#19607] Introduce `Hash#symbolize_keys`.

Issue #19607 has been reported by ioquatix (Samuel Williams). ---------------------------------------- Bug #19607: Introduce `Hash#symbolize_keys`. https://bugs.ruby-lang.org/issues/19607 * Author: ioquatix (Samuel Williams) * Status: Open * Priority: Normal * Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- This is a very common operation. It can currently be implemented using `Hash#transform_keys(&:to_sym)`. It's currently provided by Rails as `Hash#symbolize_keys` and `Hash#symbolize_keys!`. Proposed implementation is identical to Rails implementation: https://github.com/rails/rails/blob/539144d2d61770dab66c8643e744441e52538e09... For completeness we could also consider adding `stringify_keys` but I think that's less frequently used. -- https://bugs.ruby-lang.org/

Issue #19607 has been updated by ioquatix (Samuel Williams). One part of my motivation for creating this issue is to bring attention to the following inconsistencies: - https://bugs.ruby-lang.org/issues/15541 - https://bugs.ruby-lang.org/issues/19591 I personally agree with the OP whom stated that `symbolize_keys` is a much more intuitive option name. ---------------------------------------- Feature #19607: Introduce `Hash#symbolize_keys`. https://bugs.ruby-lang.org/issues/19607#change-102844 * Author: ioquatix (Samuel Williams) * Status: Open * Priority: Normal ---------------------------------------- This is a very common operation. It can currently be implemented using `Hash#transform_keys(&:to_sym)`. It's currently provided by Rails as `Hash#symbolize_keys` and `Hash#symbolize_keys!`. Proposed implementation is identical to Rails implementation: https://github.com/rails/rails/blob/539144d2d61770dab66c8643e744441e52538e09... For completeness we could also consider adding `stringify_keys` but I think that's less frequently used. -- https://bugs.ruby-lang.org/

Issue #19607 has been updated by ioquatix (Samuel Williams). I did some more research. The actual JSON RFC https://www.rfc-editor.org/rfc/rfc8259 refers to `name/value` pairs. So `symbolize_names` is not so strange for `JSON.parse`. In the YAML RFC https://yaml.org/spec/1.2.2/#mapping refers to it as `key/value` pairs. So `symbolize_keys` can be more natural, according to the spec, but is inconsistent with JSON. In addition, YAML keys can be different data types e.g. integer, string, symbol, timestamp. In this case, it's not symbolizing all the keys, but just ones which represent names (strings). So, maybe that's also acceptable. Therefore, I think that those option names are unrelated to `symbolize_keys` as a method name. But we should not use those options as justification, and instead look for the specific circumstances in each case. That being said, consistency is also useful. ---------------------------------------- Feature #19607: Introduce `Hash#symbolize_keys`. https://bugs.ruby-lang.org/issues/19607#change-102847 * Author: ioquatix (Samuel Williams) * Status: Open * Priority: Normal ---------------------------------------- This is a very common operation. It can currently be implemented using `Hash#transform_keys(&:to_sym)`. It's currently provided by Rails as `Hash#symbolize_keys` and `Hash#symbolize_keys!`. Proposed implementation is identical to Rails implementation: https://github.com/rails/rails/blob/539144d2d61770dab66c8643e744441e52538e09... For completeness we could also consider adding `stringify_keys` but I think that's less frequently used. -- https://bugs.ruby-lang.org/

Issue #19607 has been updated by nobu (Nobuyoshi Nakada). ioquatix (Samuel Williams) wrote:
This is a very common operation.
I have never needed such operation. What’s the use case? ---------------------------------------- Feature #19607: Introduce `Hash#symbolize_keys`. https://bugs.ruby-lang.org/issues/19607#change-102853 * Author: ioquatix (Samuel Williams) * Status: Open * Priority: Normal ---------------------------------------- This is a very common operation. It can currently be implemented using `Hash#transform_keys(&:to_sym)`. It's currently provided by Rails as `Hash#symbolize_keys` and `Hash#symbolize_keys!`. Proposed implementation is identical to Rails implementation: https://github.com/rails/rails/blob/539144d2d61770dab66c8643e744441e52538e09... For completeness we could also consider adding `stringify_keys` but I think that's less frequently used. -- https://bugs.ruby-lang.org/

Issue #19607 has been updated by ioquatix (Samuel Williams). https://github.com/search?q=repo%3Arails%2Frails%20symbolize_keys&type=code or more generally: https://github.com/search?q=.symbolize_keys&type=code ---------------------------------------- Feature #19607: Introduce `Hash#symbolize_keys`. https://bugs.ruby-lang.org/issues/19607#change-102854 * Author: ioquatix (Samuel Williams) * Status: Open * Priority: Normal ---------------------------------------- This is a very common operation. It can currently be implemented using `Hash#transform_keys(&:to_sym)`. It's currently provided by Rails as `Hash#symbolize_keys` and `Hash#symbolize_keys!`. Proposed implementation is identical to Rails implementation: https://github.com/rails/rails/blob/539144d2d61770dab66c8643e744441e52538e09... For completeness we could also consider adding `stringify_keys` but I think that's less frequently used. -- https://bugs.ruby-lang.org/

Issue #19607 has been updated by nobu (Nobuyoshi Nakada). ioquatix (Samuel Williams) wrote in #note-5:
https://github.com/search?q=repo%3Arails%2Frails%20symbolize_keys&type=code
Of course it's imaginable that method is used in Rails since ActiveSupport implements it. My question is that "is it needed frequently without Rails/ActiveSupport?".
They are not copies? ---------------------------------------- Feature #19607: Introduce `Hash#symbolize_keys`. https://bugs.ruby-lang.org/issues/19607#change-102856 * Author: ioquatix (Samuel Williams) * Status: Open * Priority: Normal ---------------------------------------- This is a very common operation. It can currently be implemented using `Hash#transform_keys(&:to_sym)`. It's currently provided by Rails as `Hash#symbolize_keys` and `Hash#symbolize_keys!`. Proposed implementation is identical to Rails implementation: https://github.com/rails/rails/blob/539144d2d61770dab66c8643e744441e52538e09... For completeness we could also consider adding `stringify_keys` but I think that's less frequently used. -- https://bugs.ruby-lang.org/

Issue #19607 has been updated by ioquatix (Samuel Williams). The common implementation `transform_keys(&:to_sym)` seems fairly common. https://github.com/search?q=%22transform_keys%28%26%3Ato_sym%29%22&type=code and https://github.com/search?q=%22transform_keys%21%28%26%3Ato_sym%29%22&type=code ---------------------------------------- Feature #19607: Introduce `Hash#symbolize_keys`. https://bugs.ruby-lang.org/issues/19607#change-102857 * Author: ioquatix (Samuel Williams) * Status: Open * Priority: Normal ---------------------------------------- This is a very common operation. It can currently be implemented using `Hash#transform_keys(&:to_sym)`. It's currently provided by Rails as `Hash#symbolize_keys` and `Hash#symbolize_keys!`. Proposed implementation is identical to Rails implementation: https://github.com/rails/rails/blob/539144d2d61770dab66c8643e744441e52538e09... For completeness we could also consider adding `stringify_keys` but I think that's less frequently used. -- https://bugs.ruby-lang.org/

Issue #19607 has been updated by janosch-x (Janosch Müller). nobu (Nobuyoshi Nakada) wrote in #note-4:
I have never needed such operation. What’s the use case?
dealing with data from uncontrolled sources, supporting multiple sources of config data (e.g. YML, JSON, code etc.), and making a less picky interface in general. some examples: - [faker interface](https://github.com/faker-ruby/faker/blob/6aae8e21e1eaf9ae845017a86c62ae44c9d...) - [pg config](https://github.com/ged/ruby-pg/blob/2e663b34745c9a91610bfb58b42cd6ecafc2331f...) - [redis config](https://github.com/redis/redis-rb/blob/4e9d73d3bb47831fe720cbce7c47cb11dd3f4...) - [solargraph interface](https://github.com/castwide/solargraph/blob/d48f3963d1ce19b369efbc7f0327b277...) - [interface in ruby/optparse](https://github.com/ruby/ruby/blob/39bbbd767d6465080c6e1d6ba0da98c2762eedc6/l...) (this is btw another precedent/occurrence of `symbolize_names` outside JSON and Psych). ---------------------------------------- Feature #19607: Introduce `Hash#symbolize_keys`. https://bugs.ruby-lang.org/issues/19607#change-102867 * Author: ioquatix (Samuel Williams) * Status: Open * Priority: Normal ---------------------------------------- This is a very common operation. It can currently be implemented using `Hash#transform_keys(&:to_sym)`. It's currently provided by Rails as `Hash#symbolize_keys` and `Hash#symbolize_keys!`. Proposed implementation is identical to Rails implementation: https://github.com/rails/rails/blob/539144d2d61770dab66c8643e744441e52538e09... For completeness we could also consider adding `stringify_keys` but I think that's less frequently used. -- https://bugs.ruby-lang.org/
participants (3)
-
ioquatix (Samuel Williams)
-
janosch-x
-
nobu (Nobuyoshi Nakada)