Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can anyone explain this array declaration to me?

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

like image 520
penger Avatar asked Nov 24 '09 16:11

penger


People also ask

How do you declare an array explain?

You can declare an array of arrays (a "multidimensional" array) by following the array declarator with a list of bracketed constant expressions in this form: type-specifier declarator [ constant-expression ] [ constant-expression ] ...

What is array explain with suitable example?

An array is a variable that can store multiple values. For example, if you want to store 100 integers, you can create an array for it. int data[100];

What is an array explain in how many ways array can be created?

One-Dimensional Arrays: type declares the element type of the array. The element type determines the data type of each element that comprises the array. Like an array of integers, we can also create an array of other primitive data types like char, float, double, etc., or user-defined data types (objects of a class).

What is the difference between array definition and array declaration?

A definition of an identifier is a declaration for that identifier that: As you noted yourself the memory for the array is reserved in data section then you have a declaration of an array that at the same time is a definition. Save this answer.


1 Answers

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).

like image 187
mipadi Avatar answered Oct 13 '22 16:10

mipadi