R.I.P. Ruby Hash Rocket Syntax 1993-2010
blog.peepcode.com
R.I.P. Ruby Hash Rocket Syntax 1993-2010
1–10 of 95 posts
Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010
#2Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010
#3Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010
#4Now if only more gems were 1.9 compatible.
Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010
#5 {
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 stringRe: R.I.P. Ruby Hash Rocket Syntax 1993-2010
#6Am I reading this right that it only applies if your hash key is a symbol? So really, the hash rocket isn't dead?
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
#7Am I reading this right that it only applies if your hash key is a symbol? So really, the hash rocket isn't dead?
Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010
#8Re: R.I.P. Ruby Hash Rocket Syntax 1993-2010
#9Am I reading this right that it only applies if your hash key is a symbol? So really, the hash rocket isn't dead?
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
#10Am 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.