Feature #9686

Syntax for symbols used in hashes

Added by Sascha Mayr 11 months ago. Updated 11 months ago.

[ruby-core:61743]
Status:Open
Priority:Normal
Assignee:-

Description

Symbols are often used literally as keys in hashes like this:

{foo: 'bar'}

But acutally there is a little inconvenience when using the alternative :"name" literal syntax:

string = 'foo'
{:"#{string}" => 'bar'}

It would be great if Ruby provided the possibility to write the second example like this:

string = 'foo'
{"#{string}": 'bar'}

This would be logical, because you can write both syntaxes the same way when not using them in hashes.

History

#1 Updated by Jeremy Kemper 11 months ago

See #4276

#2 Updated by Nobuyoshi Nakada 11 months ago

  • Description updated (diff)

You can write simply for that case:

string = 'foo'
{string => 'bar'}

#3 Updated by Matthew Kerwin 11 months ago

On Mar 29, 2014 8:52 AM, nobu@ruby-lang.org wrote:

You can write simply for that case:

string = 'foo'
{string => 'bar'}

You'd need

 {string.to_s => 'bar'}

#4 Updated by Nobuyoshi Nakada 11 months ago

Sorry, string.to_sym.

Also available in: Atom PDF