Feature #9980

Create HashWithIndiferentAccess using new syntax {a: 1}i

Added by Rodrigo Rosenfeld Rosas over 1 year ago. Updated about 1 year ago.

Assignee:Yukihiro Matsumoto


This is related to feature #7797. For that feature I requested a regular hash ({}) to be treated as HWIA and the current hash implementation would be available as StrictHash.new since it's not as much needed as the common case of expecting a[:a] to be the same as a['a'].

Although I still prefer the proposal in #7797, I'd like to propose an alternative syntax for a new implementation of Hash that converts symbols to strings in keys (or keys to strings, it doesn't matter). This:

{my_key: 'my_value'}i

Should be equivalent to:

HashWithIndifferentAccess.new(my_key: 'my_value') # from ActiveSupport

It doesn't have to behave exactly like the implementation from ActiveSupport. I only really care about symbols and strings being treated the same way.

This is not as great as #7797 because it will only affect user's own code rather than hashes created by other libraries (unless they decide to adopt the new syntax, which could take quite a while to keep backward compatibility with old Ruby versions). But it would already make it way more pleasant to define new hashes with indifferent access.

Maybe it could act recursively during the hash creation, but I'm fine if you think it would be more confusing than helpful. To illustrate:

{a: {b: 1}}i

could mean the same as:

{a: {b: 1}i}i

feature-9980.pdf - Slide for proposal (newest from Firefox) (24.8 KB) Rodrigo Rosenfeld Rosas, 06/30/2014 05:18 PM


#1 Updated by Rodrigo Rosenfeld Rosas over 1 year ago

  • File feature-9980.pdf added

#2 Updated by Yui NARUSE over 1 year ago

received, thanks!

#3 Updated by Rodrigo Rosenfeld Rosas over 1 year ago

Reattaching using Firefox

#4 Updated by Yui NARUSE about 1 year ago

  • File deleted (feature-9980.pdf)

#5 Updated by Yukihiro Matsumoto about 1 year ago

  • Status changed from Open to Rejected

Suffix i is used for complex (imaginary) numbers. It's bit confusing.
Maybe what you want can be gained by shorter/nicer name for Hash#compare_by_identity.


#6 Updated by Rodrigo Rosenfeld Rosas about 1 year ago

Was this rejected because the i suffix was already taken or because the idea of using suffixes would be confusing? Should I try another suffix in another ticket?

I don't think compare_by_identity does what I want. It seems to behave similar to ActiveSupport Hash#symbolize_keys which is different from Hash#with_indifferent_access which is what I really want.

I don't want to have to worry if I should call hash[:a] or hash['a']. Also compare_by_identity is a bad feature to me as it modifies the way the original hash works (it should be called compare_by_identity! in my opinion) and you can't undo its effect.

Maybe Ruby core could provide some utility to return a HWIA from a hash, like H(a: 1) meaning an equivalent of HWIA.new(a: 1).

Also available in: Atom PDF