Quoted Labels: A Reasonable Ruby Syntax Change

Posted by -

Inspired by the discussion in ruby-core:36559 (redmine link: Feature #4801), personal experience using the label syntax, and my dabbling with converting between label syntaxes, I felt like quoted symbols have a place with label syntax. In essence, the following two code blocks should be equivalent, in my opinion:

Ruby 1.8

  x = 'hello'
  h = {:foo => 1, :'use-19' => true, :"#{x}-world" => 3.14}

Proposed Ruby 1.9/2.0

  x = 'hello'
  h = {foo: 1, 'use-19': true, "#{x}-world": 3.14}

It's just the extension of the quoted symbol forms to the label syntax. We already have labels, and they're here to stay. Without adding quoted forms, you end up with hashes like this:

  h = {foo: 1, :'use-19' => true}

Naturally, the solution is to replace foo: with :foo => - invalidating the label syntax. While I understand that being okay if you don't like labels, I feel that if labels are to be a first-class citizen in Ruby, they should be handled properly. Matz seemed to agree in the thread I listed.

The Solution [Patch]

I've prepared a patch that implements this behavior! You can download it here.. I've also posted a Feature on the Ruby redmine, but it hasn't gotten much attention, so I figured I'd put this out to the world here, in case it dies on the vine there.

The fun part was actually that, in my implementation, I had to manually reject all string delimiters except ' and "; for a while, I had an implementation that allowed the following...

  h = {foo: 1, %q@use-19@: 2}

Which is just plain weird. Fun, but weird. Too weird to actually suggest it land in the language.

However, I'm starting to play with the idea of combining RVM with a repository of one-off patches to Ruby mainline. RVM already has a feature for adding custom patches. Having written many such patches and seen others implement cool stuff (such as Shugo Maeda's refinements patch (link to patch itself), it could open up a lot of possibilities. I doubt anyone would rely on it for a production deployment, but it's fun to have a neat little feature in your Ruby like that. Having a repository/browser for patches could make that more useful. I'm thinking even just a well-known github repository would be enough.

Either way, if you want the unrestricted patch, it's here.

Thoughts?

What are your thoughts on such a syntax change? Most of my Ruby work doesn't affect the whole community, so I'm curious what others think of this one.

I've added a small bit of commenting code to this blog, prompted by this post. You can login with Twitter, Google, or any other OpenID provider to comment below. No credentials are saved so you won't be able to edit/delete your comments until someone bugs me enough to add that as a feature.


Enjoy this article? Then feel free to:


Comments