Re:R.I.P. Ruby Hash Rocket Syntax 1993-2010
by wfarr on Thursday January 06, 2011 @12:08AM
(#2074116)
Who would put a space inside the key name for an item in a hash?
I'm confident it's commonly understood best practice (among many languages at that) to use an underscore (_) in lieu of spaces in such things.
I agree that the quirks themselves are odd, but the matter of using a key with a space in it really stood out.
Re:R.I.P. Ruby Hash Rocket Syntax 1993-2010
by korny on Thursday January 06, 2011 @01:05AM
(#2074268)
When your key is a phrase, for instance?
A hash of { phrase => count } is a pretty common data structure.
It's really common, actually. A symbol can actually have spaces in it:
:"Some Text" is a perfectly valid symbol.
ruby-1.9.2-p136 :001 > h = {:"Some Text" => 123} => {:"Some Text"=>123} ruby-1.9.2-p136 :002 > h[ "Some Text".to_sym ] => 123
Re:R.I.P. Ruby Hash Rocket Syntax 1993-2010
by regularfry on Thursday January 06, 2011 @04:14AM
(#2074691)
You can do it, but is it a good idea? What it implies to me is that at some point in your program, you're constructing symbols from arbitrary strings which aren't known ahead of time. This sounds like a recipe for disaster given that symbols aren't garbage collected.
This does not work (although I think it could):
{"foo bar": 'b'}
but this does:
{:"foo bar" => 'b'}
Without being truly JSON compatible, it's only moderately useful.
One annoyance of Ruby for me has always been that many libraries, such as Rails, allow for symbols or strings interchangeably - but only in "most" cases, often leading to head banging in the other cases.
More intrigue: the syntaxes are also mixable
{foo: 'bar', "add-a" => 'dash', :'or a' => 'space in a symbol'}
is valid.