How to Create an A Ruby Array
In Ruby, we can create an array by enclosing a collection of comma-separated values in pair of square brackets.
For example:
Each item in an array is known as an element, and it can be of any valid object such as integer, float, string, hashes, and more.
You can also create an array in Ruby using the new class method. For example:
To define the array size during creation, you can pass the size of the array in parenthesis as:
The above example has a size of 10 elements.
How to Read Array Elements
The first step to reading an array in Ruby is printing it out. In such a case, we can use built-in ruby methods such as print and puts:
print myarray
[1, 2, 3, 4, 5, 6]
Using the print method will return the elements in an array all in a single line.
The example below uses the puts command to show the elements in the array:
puts myarray
1
...
5
6
Unlike the print method, puts will display the elements in an array, each element per line.
To iterate over the elements in an array, you can use each# function. Take the example below:
myarray.each {|i| puts i}
The above example will return each item in the array.
Another example when iterating over an array is to use a simple for loop. Consider the example shown below:
for i in myarray
puts i
end
Similar to each# method, the for loop above will return each item in the array.
How to Update Ruby Array Elements
You can update the values in a Ruby array by performing various actions. For example, you can sort them, add a new item, reverse the elements, and many more.
Let us discuss a few examples of updating an existing array in Ruby.
To add a new item to the array, you can use the push method as:
"James Kirk",
"William Riker",
"Christopher Pike",
"Jonathan Archer"
]
captains.push("Jean-Luc Picard")
The push method will take the provided value and append it at the end of the array.
Output:
["James Kirk", "William Riker", "Christopher Pike", "Jonathan
Archer", "Jean-Luc Picard"]
To add a new element at the beginning of the array, you can use the unshift method as:
print captains
=>["Kathryn Janeway", "James Kirk", "William Riker", "Christopher Pike", "Jonathan Archer"]
Suppose you want to modify the element in an array stored at a specific index? To do this, you can use the assignment operator.
Consider the example below:
print captains
["Carol Freeman", "William Riker", "Christopher Pike", "Jonathan Archer"]
In the example above, we select the element at the index of 0 and reassign a new value.
How to Delete Array Elements In Ruby
Ruby provides you with various ways to remove elements in an array. Such methods include:
Using pop# method
To remove the last element in an array, use the pop method. Consider the example shown below:
"James Kirk",
"William Riker",
"Christopher Pike",
"Jonathan Archer"
]
captains.pop
print captains
The pop method takes the last element in the array and deletes it as shown in the resulting array:
Using shift# method
The shift method is similar to the pop method. However, it removes the first element in the array as shown:
"James Kirk",
"William Riker",
"Christopher Pike",
"Jonathan Archer"
]
captains.shift
print captains
The shit method removes the element at index 0 as shown:
Using delete_at# method
If you want to remove an array at a specific index, you can use the delete_at method. It takes the index position as the argument.
Consider the example below:
"James Kirk",
"William Riker",
"Christopher Pike",
"Jonathan Archer"
]
captains.delete_at(2)
print captains
The above example removes the element stored at index 2 of the array. The resulting value is:
Using delete# method
The delete method accepts an array value and removes it from the specified array. For example:
"James Kirk",
"William Riker",
"Christopher Pike",
"Jonathan Archer"
]
captains.delete("William Riker")
print captains
Unlike delete_at, the delete method accepts an actual value of the array.
Using Subtract and Assignment operator
The subtract and assignment operator expressed as -= can remove an element from an array.
Consider the example shown below:
"James Kirk",
"William Riker",
"Christopher Pike",
"Jonathan Archer"
]
captains -= ["Christopher Pike"]
print captains
The example above specifies the element to remove inside a pair of square brackets.
Using delete_if# method
The delete_if method accepts a conditional and removes all the elements in the array that do not meet the specified condition.
Take the example below:
"James Kirk",
"William Riker",
"Christopher Pike",
"Jonathan Archer"
]
captains.delete_if { |i| i.length <11}
print captains
The method takes a block and evaluates each element for a matching case. If the value does not meet the set conditions, the method removes it.
The resulting value is:
Final Thoughts
In this guide, we discussed CRUD operations when working with arrays in Ruby. It is good to note that this tutorial does not exhaust all the methods and techniques for CRUD operations when working with Ruby arrays.
Thank you for reading!