Project

General

Profile

Actions

Feature #7797

closed

Hash should be renamed to StrictHash and a new Hash should be created to behave like AS HashWithIndifferentAccess

Added by rosenfeld (Rodrigo Rosenfeld Rosas) about 11 years ago. Updated over 9 years ago.

Status:
Rejected
Target version:
[ruby-core:51977]

Description

Since #7792 has been rejected (although I don't really understand the reason except people being afraid of changing I guess) I'd like to propose an alternative solution to most of the problems caused by the differences between symbols and strings.

From my previous experience, most of the time I'm accessing a hash, I'd prefer that it behaved like HashWithIndifferentAccess (HWIA from now) from active_support gem.

Transforming all possible hashes in some object to HWIA is not only boring to do code but also time consuming.

Instead, I propose that {}.class == Hash, with Hash being implemented as HWIA and the current Hash implementation renamed to StrictHash.

That way, this should work:

a = {a: 1, 'b' => 2}
a[:a] == a['a'] && a['b'] == a[:b]

I don't really see any real use case where people really want to have a hash like this:

h = {a: 1, 'a' => 2}

This would only confuse people.

It also avoids confusion when parsing/unparsing from popular serialization formats, like JSON:

currently:

h = {a: 1}
j = JSON.unparse h
h2 = JSON.parse j
h[:a] != h2[:a]

With the new proposition (I'm assuming JSON should use Hash instead of StrictHash when parsing) h[:a] == h2[:a].

This is just a small example but most real-world usage for hashes would benefit from regular hashes behaving like HWIA.


Files

redmine-bug.jpg (107 KB) redmine-bug.jpg Redmine attachment bug rosenfeld (Rodrigo Rosenfeld Rosas), 06/30/2014 05:04 PM
feature-7797.pdf (29.3 KB) feature-7797.pdf Slide for proposal (newest from Firefox) rosenfeld (Rodrigo Rosenfeld Rosas), 06/30/2014 05:13 PM

Related issues 1 (0 open1 closed)

Has duplicate Ruby master - Feature #8939: symbol / string invariance (for hashes)Closed09/23/2013Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0