Feature #9980

Create HashWithIndiferentAccess using new syntax {a: 1}i

Added by rosenfeld (Rodrigo Rosenfeld Rosas) about 3 years ago. Updated about 3 years ago.

Target version:


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 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: '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 (24.8 KB) feature-9980.pdf Slide for proposal (newest from Firefox) rosenfeld (Rodrigo Rosenfeld Rosas), 06/30/2014 05:18 PM


#1 [ruby-core:63339] Updated by rosenfeld (Rodrigo Rosenfeld Rosas) about 3 years ago

  • File feature-9980.pdf added

#2 [ruby-core:63424] Updated by naruse (Yui NARUSE) about 3 years ago

received, thanks!

#4 [ruby-core:64026] Updated by naruse (Yui NARUSE) about 3 years ago

  • File deleted (feature-9980.pdf)

#5 [ruby-core:64028] Updated by matz (Yukihiro Matsumoto) about 3 years 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 [ruby-core:64097] Updated by rosenfeld (Rodrigo Rosenfeld Rosas) about 3 years 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 1).

Also available in: Atom PDF