When you're making a hash in Ruby, you can add a trailing comma with no problems.
E.g.:
>> h = {:x => :y,}
=> {:x=>:y}
The benefit here is that you can then add subsequent key-value pairs without ever getting those lame "oops I forgot a comma" errors.
Thursday, October 18, 2007
Subscribe to:
Post Comments (Atom)













Supported in Rubinius and JRuby?
ReplyDeleteAnswering my own question: doesn't look like it.
ReplyDeletehttp://pastie.caboo.se/108495
Okay, so it actually does mostly work in Rubinius.
ReplyDeletehttp://pastie.caboo.se/108503
Works with lists, too:
ReplyDelete>> foo = [1,2,]
=> [1, 2]
Wha? I could have sworn I tried that at least once before and it didn't work. Good to know it does the right thing after all.
ReplyDeleteRubinius and JRuby are both bleeding-edge; if they lack the implementation, that's something to add.
ReplyDelete@piers - it **doesn't** work without the curly braces. so if you use Ruby's "invisible curly braces" then you can't use this trick. but if you are using curly braces it's a good habit to be in.
Internet Explorer will hate you if you do the same with javascript.
ReplyDeleteInternet Explorer seems filled with hate. At the minimum, it's definitely grumpy.
ReplyDelete