Project

General

Profile

Actions

Feature #9686

open

Syntax for symbols used in hashes

Added by saschamayr (Sascha Mayr) over 10 years ago. Updated almost 3 years ago.

Status:
Open
Assignee:
-
Target version:
-
[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.

Updated by nobu (Nobuyoshi Nakada) over 10 years ago

  • Description updated (diff)

You can write simply for that case:

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

Updated by phluid61 (Matthew Kerwin) over 10 years ago

On Mar 29, 2014 8:52 AM, wrote:

You can write simply for that case:

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

You'd need

{string.to_s => 'bar'}

Updated by nobu (Nobuyoshi Nakada) over 10 years ago

Sorry, string.to_sym.

Updated by googlefeud (google feud (spammer, locked)) almost 7 years ago

  • Subject changed from Syntax for symbols used in hashes to google classroom

You can solve your issue by using oogle classroom login

Actions #6

Updated by usa (Usaku NAKAMURA) almost 7 years ago

  • Subject changed from google classroom to Syntax for symbols used in hashes
Actions #7

Updated by hsbt (Hiroshi SHIBATA) almost 3 years ago

  • Project changed from 14 to Ruby master
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0