On Sep 13, 2010, at 1:47 PM, Colin Bartlett wrote:
> On Mon, Sep 13, 2010 at 4:22 AM, Rob Biedenharn
> <Rob / agileconsultingllc.com>wrote:
>
>> ... Here's a way I've thought about it:
>> Picture the array with the comma after each element ...
>
> That may - or may not! - explain this behaviour: I've been wondering  
> why the
> statements with 1 (but not 2) 'superfluous' commas at the end don't  
> give a
> syntax error:
> arr = []           #=> []
> arr = [ 0 ]          #=> [0]
> arr = [ 0, ]         #=> [0]; superfluous comma is ignored
> arr = [ 0, 1, 2 ]  #=> [0, 1, 2]
> arr = [ 0, 1, 2, ] #=> [0, 1, 2]; superfluous comma is ignored
> but these give: syntax error, unexpected ',', expecting ']'
> # arr = [ 0, 1, 2, 3,, ]
> # arr = [ ,  ]


Well, given that Matz is a C programmer (as was I), he may have wanted  
to allow:

<code lang="ruby">
arr = [
   0,
   1,
   2,
   3,
]
</code>

When making C array literals, I would often do:

<code lang="C">
int arr[] = {
    0
   ,1
   ,2
   ,3
};
</code>

because it was much more likely to add another element to the end than  
to the beginning of the array.  In C, you couldn't have a trailing  
comma and in ruby, there has to be something to clue the parser into  
the fact that there's more to come on the next line.

-Rob

Rob Biedenharn		
Rob / AgileConsultingLLC.com	http://AgileConsultingLLC.com/
rab / GaslightSoftware.com		http://GaslightSoftware.com/