Project

General

Profile

Feature #9686

Syntax for symbols used in hashes

Added by Sascha Mayr almost 2 years ago. Updated almost 2 years ago.

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

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

#2 [ruby-core:61745] Updated by Nobuyoshi Nakada almost 2 years ago

  • Description updated (diff)

You can write simply for that case:

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

#3 [ruby-core:61746] Updated by Matthew Kerwin almost 2 years 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 [ruby-core:61748] Updated by Nobuyoshi Nakada almost 2 years ago

Sorry, string.to_sym.

Also available in: Atom PDF