From: Peter Hickman Date: 2010-07-07T22:59:17+09:00 Subject: Re: Hashes don't allow preceding commas by design? --001485f5b02e2f7dc4048acc93ed Content-Type: text/plain; charset=ISO-8859-1 On 7 July 2010 13:43, Brian Candler wrote: > But I think the question is why the following isn't allowed: > > { > :first => 1 > , > :second => 2 > } > > Look at this in irb might help us understand it irb(main):001:0> a = { irb(main):002:1* :first => 1 irb(main):003:1> , irb(main):004:1* :second => 2 irb(main):005:1> } SyntaxError: compile error (irb):3: syntax error, unexpected ',', expecting '}' from (irb):4 from :0 The irb prompt ends with a > if it is looking for a new line. When it ends with a *, as in line 2, it means that the parser thinks that the pervious line is incomplete and that line 2 is a continuation of line 1. However after entering line 2 the prompt returns to a >. This means that the parser thinks that line 2 was complete and there is nothing more to be read. Compare it to this irb(main):001:0> a = { irb(main):002:1* :first => 1, irb(main):003:1* :second => 2 irb(main):004:1> } The comma is on the end line 2 and the prompt for line 3 ends with a *. The parser shows us that it does not consider line 2 to be complete and that line 3 will be treated as a continuation of line 2. A line ending with a comma tells the parser that (in the cases where a comma is used such as hashes, lists or functions) that the statement is incomplete and to treat the next line as a continuation of this line. I'm not saying that the parser could not be written to handle the case you present it is just that with the present design that uses the end of line as an implicit end of statement marker the behaviour you are seeing is the expected behaviour. The same thing happens with the if statement if some_test || another_test will parse the same as if some_test || another_test but if some_test || another_test is something else entirely. "if some_test" on a line by itself is a complete statement so the parser is expecting the statement block (or a "then") but "if some_test ||" is incomplete as || needs something on the right hand side so it treats the next line as a continuation of the current one. --001485f5b02e2f7dc4048acc93ed--