Project

General

Profile

Feature #4935

Quoted Label Form for 1.9 Hashes

Added by adgar (Michael Edgar) over 8 years ago. Updated about 5 years ago.

Status:
Closed
Priority:
Normal
Target version:
[ruby-core:37553]

Description

As discussed in the "Shorthand Hash Syntax for Strings" thread/feature request, several (including Matz) expressed interest in a parallel quoted form for the 1.9 label syntax:

{ foo: bar }
{ 'only-19': true }

x = "hello"
{ "#{x}-world": Date.now }

This personally has come up for me with the new syntax: especially when dealing with command-line options, which often use hyphens, the quoted symbol form is necessary. Not being able to use the new syntax results in funny-looking hashes:

{ foo: bar, baz: qux, :'abc-def' => 1 }

I've attached a patch implementing this change. It allows a colon to follow single-quoted or double-quoted string literals to form a symbol key much like the label syntax introduced in 1.9. The examples listed at the start of this post all work fine. It introduces a new token - tLABEL_END - which replaces tSTRING_END when closing a symbol-label-literal. It introduces a single new grammar production for assoc, which is nearly purely syntactic sugar for the dsym grammar production; I've extracted dsym's implementation to a separate helper function, which is called from the dsym production and from the new label-style production.

I will immediately attach a new passing test for test_hash.rb in a followup post, once this has a feature # and ruby-core thread assigned to it.

NB: By removing the quote check on line 6580, one can make this feature work with any string literal (like "{ %q/foo-bar/: baz}", obviously doesn't work with heredocs) - I personally find this to be silly and inconsistent with the existing symbol syntax. Though it was fun to play with for a bit.


Files

label_patch.diff (4.07 KB) label_patch.diff Implementation adgar (Michael Edgar), 06/27/2011 11:28 AM
label_test.diff (541 Bytes) label_test.diff Test for the implementation of this feature. adgar (Michael Edgar), 06/27/2011 11:31 AM

Related issues

Is duplicate of Ruby master - Feature #4276: Allow use of quotes in symbol syntactic sugar for hashesClosed01/13/2011Actions

Associated revisions

Revision cbdac1ea
Added by nobu (Nobuyoshi Nakada) almost 5 years ago

parse.y: fix lex_state after tLABEL_END

  • parse.y (parser_yylex): fix lex_state after tLABEL_END, should be EXPR_LABELARG to be followed by "paren with arg". [ruby-core:66705] [Feature #4935]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48712 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 48712
Added by nobu (Nobuyoshi Nakada) almost 5 years ago

parse.y: fix lex_state after tLABEL_END

  • parse.y (parser_yylex): fix lex_state after tLABEL_END, should be EXPR_LABELARG to be followed by "paren with arg". [ruby-core:66705] [Feature #4935]

Revision 48712
Added by nobu (Nobuyoshi Nakada) almost 5 years ago

parse.y: fix lex_state after tLABEL_END

  • parse.y (parser_yylex): fix lex_state after tLABEL_END, should be EXPR_LABELARG to be followed by "paren with arg". [ruby-core:66705] [Feature #4935]

Revision 48712
Added by nobu (Nobuyoshi Nakada) almost 5 years ago

parse.y: fix lex_state after tLABEL_END

  • parse.y (parser_yylex): fix lex_state after tLABEL_END, should be EXPR_LABELARG to be followed by "paren with arg". [ruby-core:66705] [Feature #4935]

Revision 48712
Added by nobu (Nobuyoshi Nakada) almost 5 years ago

parse.y: fix lex_state after tLABEL_END

  • parse.y (parser_yylex): fix lex_state after tLABEL_END, should be EXPR_LABELARG to be followed by "paren with arg". [ruby-core:66705] [Feature #4935]

Revision 48712
Added by nobu (Nobuyoshi Nakada) almost 5 years ago

parse.y: fix lex_state after tLABEL_END

  • parse.y (parser_yylex): fix lex_state after tLABEL_END, should be EXPR_LABELARG to be followed by "paren with arg". [ruby-core:66705] [Feature #4935]

Revision 48712
Added by nobu (Nobuyoshi Nakada) almost 5 years ago

parse.y: fix lex_state after tLABEL_END

  • parse.y (parser_yylex): fix lex_state after tLABEL_END, should be EXPR_LABELARG to be followed by "paren with arg". [ruby-core:66705] [Feature #4935]

History

Updated by adgar (Michael Edgar) over 8 years ago

Sorry, meant to list this as a feature. Test attached.

#2

Updated by shyouhei (Shyouhei Urabe) over 8 years ago

  • Tracker changed from Bug to Feature

Updated by adgar (Michael Edgar) over 8 years ago

With the feature freeze coming up in a couple days, is there a chance this sees 1.9.3? It doesn't break any old syntax, only introduces new syntax, and rounds out the label feature of the 1.9 branch nicely.

Updated by mame (Yusuke Endoh) over 7 years ago

  • Status changed from Open to Assigned
  • Assignee set to matz (Yukihiro Matsumoto)

Updated by mame (Yusuke Endoh) almost 7 years ago

  • Target version set to 2.6

Updated by nobu (Nobuyoshi Nakada) about 5 years ago

  • Status changed from Assigned to Closed

Applied in changeset r47649.


parse.y: quoted ID key

  • parse.y (assoc): allow quoted ID as a key of a hash literal. [ruby-core:34453] [Feature #4276]

Also available in: Atom PDF