Updated April 3, 2023
Introduction to Ruby Arrays
Arrays in Ruby are the way to store the collection of various objects , every element in ruby array are accessed with an index value , the index value are start from 0 same as it works in case of programing in c or java ,here if we use any negative value of index which means we are talking about the end of the array for example -1 means last element and -2 means second last element of the array ,size of array in ruby are not fixed we can add as many element as we want size will automatically grow with addition of new element and these element can be integer ,String, Fixnum ,hash or any other array.
How to Create Arrays in Ruby?
There are multiple way to create array in ruby some of them are given below ,
By Using the New Class
By using a new class we can define the array , here we can define the size also for the array which we are going to use .This is the simplest way to create the array in Ruby. Here we are using the new class and assigning the length for the array elements . Simple way using new class.
Code:
students = Array.new
Defining the size for the array ,
Student =Array.new(30)
We can see the size of the array with the Ruby available methods size and length in the below way,
student = Array.new(30)
$size=student.size
$length=student.length
puts "size of array is #$size"
puts "size of array is #$length"
Output:
By Assigning the Value
We can also create an array in Ruby by assigning the value to the array of each element.In the below example we have simply used the new class with passing two argument to it , one is the length of the array and and another the element which is going to repeatedly used as the element of the array.
Code:
student = Array.new(7, "ranjan")
puts "student repeated name is #{student}"
Output:
Using Bock
We can define some block with some logic inside that block, whatever logic will be written inside the block the same array will be produced, see the example below. In the below example we have defined the block with logic of to get the square of each number starting from 0 to 11 as the length of array is 12.
Code:
Number = Array.new(12) { |e| e = e * e }
puts "array with square from 0 to 11 is #{Number}"
Output:
Using array as the function
In this way of array creation we have to pass one argument to the array as the function with start to end value sequences of the array attributes, below is the example for it. In the below example we can see that we have passed the argument for the array from 10 to 20 and it will automatically understand that it has to create the array of numbers starting from 10 and end with the 20.
Code:
numbers = Array(10..20)
puts "#{numbers}"
Output:
How to Add an Array Element in Ruby?
We can add elements to array in Ruby in many ways some of important ways are given below ,
1. Adding the Element to the end of the Array : Here in this way we are adding the array element at the end of the existing array . First we added with operator << at the end and then we used the method push to add the new attribute at the end of the array .Please see the below example along with screen of output.
Code:
student = ["ranjan", "ajay", "vijay"]
puts "older array is #{student}"
student << "sujit"
puts "first change array is #{student}"
student.push("akash"
puts "second change array is #{student}"
Output:
2. Adding the Element to the beginning of Array: This way we are adding the element to the beginning of the array .Firast we used the unshift method to add the element at the start of the array then we used the + operator to add new elements at the beginning of the array .Here we with the help of + operator we are adding two element at once .Please see the below example along with screen of output.
Code:
student = ["ranjan", "ajay", "vijay"]
puts "older array is #{student}"
student.unshift("sujit")
puts "first changed array is #{student}"
student +=["akash","vikash"]
puts "second changes array is #{student}"
Output:
Various Arrays operations in Ruby
Array are the most important thing is the ruby language as it contains the data which need to manipulate many time ,so to do so we need to perform certain operation on the array , and these operations can be looping on the array data ,adding element to the array and removing any element from the array or changing the value for the existing element of array. let’s discuss each one by one .
- In this example we are simply running a loop on the array of students data and printing them.So this is the example for the running a loop on the Ruby array.
Code:
students = ["Ranjan", "Ajay", "Vijay", "Sujoy"]
for b in students do
puts "student name is #{b}"
end
Output:
- Here we are adding “vikash” to the student array at the end, so this is an example of adding a new element into the array. We can also add at the beginning by using unshif and + operators in Ruby.
Code:
students = ["Ranjan", "Ajay", "Vijay", "Sujoy"]
students.push("vikash")
for b in students do
puts "student name is #{b}"
end
Output:
- Here we are removing the element “Ranjan” from the array of the student. We have used here the method delete of Ruby array.
Code:
students = ["Ranjan", "Ajay", "Vijay", "Sujoy"]
students.delete("Ranjan")
for b in students do
puts "student name is #{b}"
end
Output:
- In this example, we are simply changing the “Ranjan” attribute of the student’s array and replacing it with the “suresh” attributes .
Code:
students = ["Ranjan", "Ajay", "Vijay", "Sujoy"]
students[0]="suresh"
for b in students do
puts "student name is #{b}"
end
Output:
Conclusion
From these tutorials we learned about the array in the ruby language, we learned the ways to create arrays and some important operations which we need to perform many times in the real cases. We learned how to add an element in the given array of ruby which is very common for any array.
Recommended Article
We hope that this EDUCBA information on “Ruby Arrays” was beneficial to you. You can view EDUCBA’s recommended articles for more information.