Live data from Hacker News

R.I.P. Ruby Hash Rocket Syntax 1993-2010

blog.peepcode.com

1–10 of 95 posts

Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010

#4
post #2

Now if only more gems were 1.9 compatible.

It's more problematic for those that include Ruby extensions in C, but fixing a library to make it 1.9-compatible is not generally that onerous a task (the major exception to this is string encoding, which is fine in simple cases but a complete nightmare in anything complex). If enough people just write patches for a couple of gems they use, everything would be 1.9-compatible soon enough. rvm lets you install all the different Ruby versions you'll need for testing easily enough.

Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010

#5
also says this syntax is compatible with python which is not really true. if you have this in python:

    {
      key: "value",
      dr_nic: "The Comedian",
      ttl: 42
    }
python will expect there to be a variable called 'key', one called 'dr_nic', and one called 'ttl'. If you intend to do

    d['ttl']
you'll need to create a hash using a literal string

Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010

#6
post #3

Am I reading this right that it only applies if your hash key is a symbol? So really, the hash rocket isn't dead?

Ruby 1.9 still uses hash rocket. I'm using RoR3 on 1.9.2 and I haven't changed any of my hashes.

I guess if you want to save some key strokes and you use symbols as keys you can use the new syntax.

The other cool/different thing about Hash in 1.9 is that it preserves order.

Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010

#9
post #3

Am I reading this right that it only applies if your hash key is a symbol? So really, the hash rocket isn't dead?

Correct. It is wonderful sugar, but somewhat limiting. Realistically, you need to know both syntaxes.

  irb(main):001:0> hash={:"symbol with space" => nil}
  => {:"symbol with space"=>nil}
  irb(main):002:0> hash={"symbol with space": nil}
  SyntaxError: (irb):2: syntax error, unexpected ':',  expecting tASSOC
  hash={"symbol with space": nil}
                          ^
	from /usr/bin/irb1.9.1:12:in `'
  irb(main):003:0> hash={"symbolwithoutspace": nil}
  SyntaxError: (irb):3: syntax error, unexpected ':',  expecting tASSOC
  hash={"symbolwithoutspace": nil}
                           ^
	from /usr/bin/irb1.9.1:12:in `'
  irb(main):004:0> hash={symbolwithoutspace: nil}
  => {:symbolwithoutspace=>nil}
  irb(main):005:0> hash={symbol with space: nil}
  SyntaxError: (irb):5: syntax error, unexpected tIDENTIFIER, expecting keyword_do or '{' or '('
  hash={symbol with space: nil}

Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010

#10
post #6
post #3

Am I reading this right that it only applies if your hash key is a symbol? So really, the hash rocket isn't dead?

Ruby 1.9 still uses hash rocket. I'm using RoR3 on 1.9.2 and I haven't changed any of my hashes. I guess if you want to save some key strokes and you use symbols as keys you can use the new syntax. The other cool/different thing about Hash in 1.9 is that it preserves order.

[deleted]
Post reply on HN