Today's Question:  What are you most afraid of as a programmer?        GIVE A SHOUT

Technical Article => Programming =>  Ruby

I don’t like the Ruby 1.9 hash syntax      2011-12-14 07:05:09      5,097    0

There, I said it, I don’t like it. And I don’t know why you do either.

I assume you like it anyway, everyone else I talk to seems to. My heart sank over and over again whilst I was at the recent RailsConf and saw respected rubyist after respected rubyist using the new Ruby 1.9 hash syntax in their presentations.

I just don’t get it.

But I’m not one to just moan. I plan to justify my feelings. Then maybe you can tell me why you do like it?

My friend the hash rocket

I like the hash rocket (not least because it’s a rocket) and I know it’s not going away. However, these are my reasons for disliking the new syntax.

Keys are restricted

According to this Stack Overflow answer, keys are limited to something like /^[a-zA-Z_][a-zA-Z0-9]*$/. I like using any object I choose as a key.

# Ruby 1.8 Syntax
{:this => 'syntax', 'is' => 'fun', #<And:0x10036bb58> => 'flexible'}
# Ruby 1.9 syntax
{this: 'syntax', 'will': 'cause', #<Nasty:0x10031bf85>: 'errors'}

Obviously mixing key styles is not helpful, but surely there are situations you’ve found yourself using an integer, a string with a dash in it or even an arbitrary object as a key.

It is misleading

Your input appears to be a string of some sort, but your hash keys are symbols

hash = { test: 'hash' }
# => { :test => 'hash' }
# => NoMethodError (works in JavaScript!)
# => nil
# => 'hash' (hooray!)

Similarity to other languages

I half understand the desire to make languages act similarly so swapping between them is easier. The new syntax mimics JSON which is important for Ruby, or at least Rails, developers as JavaScript is one of the languages we’ll switch to more frequently. In theory that makes sense. However, I like the distinction. I know when I am typing Ruby and will type in the same style as I and everyone else always has. As far as I can tell, changing the hash syntax to look the same as JavaScript (or other languages) has as much point as starting to write variables in camel case in Ruby.

Backwards compatibility

Obviously this new feature is not part of Ruby 1.8. It wouldn’t be a new feature if it was. However, for something so fundamental to the language I don’t understand why we need new ways of writing it that will break older versions. Particularly, if someone decides to write or update a gem to use the 1.9 syntax and no other 1.9 features, then that gem will become unavailable for anyone still using 1.8 (of which I am sure there are more than a few) for no good reason. Normally we could argue that as the language progresses and we use 1.8 less and less, that we could migrate to using this syntax only, but I don’t think that will be the case, given the restrictions I mention earlier.

Mixing it up

When writing code in teams or contributing to larger projects, code style is important and the existence of two different syntaxes inevitably means that a project will end up with both. This is poor for consistency and whilst it could be fixed with rigour, what about that one time when you want to use an object as a key? The only way is to consistently use the 1.8 syntax.

In favour of the new syntax

Ok, it’s not all bad I admit. I can think of one place that using the new syntax is better than the old one. For example:

# Ruby 1.8 syntax
before_save :do_awesome, :if => -> { its_awesome_time?( }
# Ruby 1.9 syntax
before_save :do_awesome, if: -> { its_awesome_time?( }

Excuse the contrived example, but the stabby lambda syntax, also introduced in Ruby 1.9, looks a bit silly when used with the old hash syntax and works nicely with the new one. Of course, in Ruby 1.8 we would have just:

# Ruby 1.8 syntax
before_save :do_awesome, :if => lambda { awesome_time?( }

Which looks fine again.

Am I wrong?

There seems to be plenty of support for the new syntax. Even some of my colleagues at Mint jumped straight on the new syntax as soon as we started a project on 1.9. I still don’t get it.

Someone please tell me why the hash colon (even that sounds more ridiculous) is better than the hash rocket?




Share on Facebook  Share on Twitter  Share on Google+  Share on Weibo  Share on Reddit  Share on Digg  Share on Tumblr    Delicious



No comment for this article.


Vitamin water label

By sonic0002
If you woke up tired, you need more sleep. If you woke up drooling on your desk, you need a new job. If you woke up with a headache, somewhere in clarke quay, with dried mee goreng stuck to your face, you probably need answers, and a drink. Why not this product, huh? It's got potassium and b vitamin