Project

General

Profile

Feature #13045

Passing a Hash with String keys as keyword arguments

Added by ch1c0t (Anatoly Chernow) almost 3 years ago. Updated over 2 years ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:78713]

Description

To the following method

def new request:, response:
end

I'd like to be able to pass both

hash_with_symbol_keys = { request: another_hash, response: another_hash2 }
hash_with_string_keys = { 'request' => another_hash, 'response' => another_hash2 }

I would make a lot cleaner parsing many things returned with string keys(like the output of YAML.load).

If you don't like the above feature, could you please at least consider adding Hash#symbolize_keys(like you did with Hash#transform_values)?

History

Updated by ch1c0t (Anatoly Chernow) almost 3 years ago

  • Description updated (diff)

Updated by nobu (Nobuyoshi Nakada) almost 3 years ago

  • Description updated (diff)

I'm not a fun for this idea, since Symbol and String are different.

Updated by ch1c0t (Anatoly Chernow) almost 3 years ago

Nobuyoshi Nakada wrote:

I'm not a fun for this idea, since Symbol and String are different.

They are different. However, sometimes they are treated as the same(for example, when passed as a method name to BasicObject#send).

Hashes with string keys are ubiquitous. Should this idea be accepted, processing them would become a lot simpler.

(Thank you for your corrections of my initial message.)

Updated by shevegen (Robert A. Heiler) almost 3 years ago

I would agree with a way/method to convert string-keys into symbol-keys.

I have no idea if this should be Hash#symbolize_keys or another name - perhaps
#transform_keys and allow either to_string or to_symbol conversion - but I
agree with a convenience method that could turn keys into one or the other.

Perhaps this would also reduce the need for strange things such as
HashWithIndifferentAccess. Newcomers will always wonder whether they should
use strings or symbols (or even worse, both at the same time in the same
hash).

Updated by nobu (Nobuyoshi Nakada) almost 3 years ago

Robert A. Heiler wrote:

Perhaps this would also reduce the need for strange things such as
HashWithIndifferentAccess. Newcomers will always wonder whether they should
use strings or symbols (or even worse, both at the same time in the same
hash).

First of all, why do you want to access them indifferently?
It just sounds a diabolical habit, to me.

Updated by matz (Yukihiro Matsumoto) over 2 years ago

  • Status changed from Open to Rejected

Rejected. method to convert string-keys into symbol-keys or transform_keys may be good ideas.

Matz.

Also available in: Atom PDF