2018-10-04 14:47:55 +01:00
---
title: Ruby Arrays
---
## Ruby Arrays
An array represents a list of values. The individual values are often called "elements" of the array. To make an array in Ruby, use square brackets and separate values with commas:
```ruby
my_array = [1, 2, 3, 4, 5]
```
2018-11-19 22:09:50 -06:00
You can also create an empty array by using the 'new' method in the 'Array' class:
```ruby
my_array = Array.new
```
2018-10-04 14:47:55 +01:00
That first example is an array of numbers, but a Ruby array can contain values of different types, even other arrays:
```ruby
mixed_array = [5, "Hello World", true, [1,2,3]]
```
You can access the elements of an array with square brackets and numerical indexes. Notice that the first element is at index 0, not 1:
```ruby
mixed_array[0] # 5
mixed_array[1] # "Hello World"
mixed_array[2] # true
```
You can check how many elements an array has with the `length` method:
```ruby
2019-06-23 09:42:49 -03:00
mixed_array.length # 4
2018-10-04 14:47:55 +01:00
[].length # 0
```
You can check the first element of an array with the `first` method:
2019-06-23 09:42:49 -03:00
2018-10-04 14:47:55 +01:00
```ruby
mixed_array.first # 5
```
You can check the last element of an array with the `last` method:
2019-06-23 09:42:49 -03:00
2018-10-04 14:47:55 +01:00
```ruby
2019-06-23 09:42:49 -03:00
mixed_array.last # [1,2,3]
2018-10-04 14:47:55 +01:00
```
#### More Information:
2019-06-23 09:42:49 -03:00
[Ruby array documentation ](https://ruby-doc.org/core-2.4.2/Array.html )