Home » Ruby Arrays

Ruby Arrays

Ruby arrays are ordered collections of objects. They can hold objects like integer, number, hash, string, symbol or any other array.

Its indexing starts with 0. The negative index starts with -1 from the end of the array. For example, -1 indicates last element of the array and 0 indicates first element of the array.


Creating Ruby Arrays

A Ruby array is created in many ways.

  • Using literal constructor []
  • Using new class method

Using literal construct []

A Ruby array is constructed using literal constructor []. A single array can contain different type of objects.

For example, following array contains an integer, floating number and a string.

Output:

Ruby Arrays 1


Using new class method

A Ruby array is constructed by calling ::new method with zero, one or more than one arguments.

Syntax:

To set the size of an array,

Syntax:

Here, we have mentioned that array size is of 10 elements.

To know the size of an array, either size or length method is used.

Example:

Output:

Ruby Arrays 2

Example:

Output:

Ruby Arrays 3


Accessing Array Elements

Ruby array elements can be accessed using #[] method. You can pass one or more than one arguments or even a range of arguments.

Example:

Output:

Ruby Arrays 4

at method

To access a particular element, at method can also be used.

Example:

Output:

Ruby Arrays 5

slice method

The slice method works similar to #[] method.

fetch method

The fetch method is used to provide a default value error for out of array range indices.

Example:

Output:

Ruby Arrays 6

Example:

Output:

Ruby Arrays 7

first and last method

The first and last method will return first and last element of an array respectively.

Example:

Output:

Ruby Arrays 8

take method

The take method returns the first n elements of an array.

Example:

Output:

Ruby Arrays 9

drop method

The drop method is the opposite of take method. It returns elements after n elements have been dropped.

Example:

Output:

Ruby Arrays 10


Adding Items to Array

Ruby array elements can be added in different ways.

  • push or <<
  • unshift
  • insert

push or <<

Using push or <<, items can be added at the end of an array.

Example:

Output:

Ruby Arrays 11


unshift

Using unshift, a new element can be added at the beginning of an array.

Example:

Output:

Ruby Arrays 12


insert

Using insert, a new element can be added at any position in an array. Here, first we need to mention the index number at which we want to position the element.

Example:

Output:

Ruby Arrays 13


Removing Items from Array

Ruby array elements can be removed in different ways.

  • pop
  • shift
  • delete
  • uniq

pop

Using pop, items can be removed from the end of an array. It returns the removed item.

Example:

Output:

Ruby Arrays 14


shift

Using shift, items can be removed from the start of an array. It returns the removed item.

Example:

Output:

Ruby Arrays 15


delete

Using delete, items can be removed from anywhere in an array. It returns the removed item.

Example:

Output:

Ruby Arrays 16


uniq

Using uniq, duplicate elements can be removed from an array. It returns the remaining array.

Example:

Output:

Ruby Arrays 17


Next TopicRuby hashes

You may also like