views:

118

answers:

2

Hi guys, just wondering the difference between the presence of the last comma in the array, if there is any at all

>> [1,2,3]
=> [1, 2, 3]

>> [1,2,3,]
=> [1, 2, 3]

The second array still works, no exception raised

Thanks

+5  A: 

There's no difference. In Ruby, you're free to add a trailing comma to an array. It makes syntax like this:

a = [
  1,
  2,
  3,
]

A bit nicer, in some cases (e.g., if you want to add an element, you simply add a 4, line and don't have to worry about checking for a comma on the last line).

mipadi
it doesn't add null or anything? Array length is the same w/ or w/o the comma?
Allen
No, no null value is added -- the array just contains the elements 1,2,3 in either case.
mipadi
I guess there is no difference really eh? It really is just out of curiosity, I was coding and found something like this so I guess I would ask all you smart folks here.
penger
Nope, no difference.
mipadi
A: 

It isn't a error, just a empty value(ignored by the compiler), but I suggest you to read Understanding Ruby Arrays

Nathan Campos
Where exactly does that link mention optional trailing comma?
Mladen Jablanović