Updated March 31, 2023
What is Ruby Array Methods?
Ruby array methods are the predefined function which performs certain tasks with ease, so, for example, suppose we have a large array of students data we want to add some more students or we want to add few more students, so perform these kinds of activity there are methods for array in the Ruby, the main advantage of using the predefined set of methods for the array are we do not require to write too much code to achieve certain goals(as each function contains the specific logic inside it) also these moths are already optimizes so the performance of the application will also be good.
Ruby Array Methods
There are various methods, let us discuss each with the example,
1. new
We can create an empty array with the help of a new class of ruby.in the below example we are creating a blank array and assigning some value to the array. Follow the example below along with the output screen.
Code:
array1 = Array.new(3, true)
array2 = Array.new(3,"ranjan")
puts "Data of array1 #{array1}"
puts "Data of array2 #{array2}"
Output:
2. Try_convert
This method will be used to convert objects into the array value. In case if it will not be able to convert it will return the nil. In the below example we are shown to conversion for the object and conversion for the string and we can see it failed to convert the string but able to convert an object into an array. Follow the example below along with the output screen.
Code:
array1 = Array.try_convert(["ranjan","ajay","vijay"])
array2 = Array.try_convert("ranjan")
if array1
puts "Data after conversion for the array1 is #{array1}"
else
puts "Data conversion failed for the array1"
end
if array2
puts "Data after conversion failed for the array2 is #{array2}"
else
puts "Data conversion failed for the array2"
end
Output:
3. <<
This operator is used to add a new element into the existing array, it is basically at the end of the array. In the below example we are adding some new attributes for the are. Follow the example below along with the output screen.
Code:
student =[ "ranjan", "ajay" ]
student << "vijay"
puts "Original array is #{student}"
#on adding it returns the new array itself so we can chaining on it.
student << "akash" << "vikash" << "suresh"
puts "Appended array is #{student}"
Output:
4. <=>
This operator is used to compare one array with another array. Basically it returns [-1,0,+1], which means if the array is less then the array with which we are comparing is smaller it will return -1, and if the array is equal then it will return 0 and in case if the array is bigger it will return +1. In the below example we can see that we are comparing the array. Follow the example below along with the output screen.
Code:
output1 =[ "ranjan", "ajay", "vijay" ] <=> [ "ranjan", "vijay", "ajay" ]
output2 = [ 12,11,1,2,3,8,10 ] <=> [ 1, 2 ]
output3 = ["ranjan","ajay"] <=> ["ranjan","ajay"]
puts output1
puts output2
puts output3
Output:
5. clear
In case if we wanted to make empty to all array elements of the array then we can use this attribute. In the below example we can see that we are using the clear command to clear all the attributes of the array. Follow the example below along with the output screen.
Code:
student = [ "ranjan","vijay","ajay" ]
puts student
student.clear
if student.length>0
puts student
else
puts "student array is cleared"
end
Output:
6. delete
In case if you wanted to remove one particular element from the array then we can use this attribute. In case if the index which we are trying to delete is not available it will return nil. Follow the example below along with the output screen.
Code:
student = [ "ajay", "bikash", "bisnu", "suraj"]
student.delete("ajay")
puts student
Output:
7. Delete_if
This we use for the conditional delete. Here, in this case, it will delete every element from the array for the block logic which we will write. In the below example we can see the delete_if example. Follow the example below along with the output screen.
Code:
numbers = [ 1,2,3,4,5,6,7,8,9,10 ]
numbers.delete_if {|x| x >= 5 }
puts numbers
Output:
8. each
It will allow us to traverse through the all key-value pair for a given array. In the below example we are printing all the students’ names with ==== as the separator between them. Follow the example below along with the output screen.
Code:
students = [ "ajay", "bikash", "rakesh" ]
students.each {|student| print student ," ==== " }
Output:
9. empty
It will check for the data into the array, it will return true in case if there is no data into the array ..Follow the example below along with the output screen.
Code:
puts [].empty?
puts ["ranjan"].empty?
Output:
10. fetch
This method will be used to fetch the value for the given key. In the below example we are fetching students from the index value like for 0 and 1. Follow the example below along with the output screen.
Code:
student = [ "ranjan", "ajay", "vijay", "suraj" ]
puts student.fetch(1)
puts student.fetch(2)
puts student[0]
Output:
11. join
This method is used to convert the array into the string from each element of the array. In the below example we are taking an array with some string and once we run the join it will return a full string with joining all of the letters. Follow the example below along with the output screen.
Code:
array= [ "r", "a", "n", "j", "a" ,"n" ]
string =array.join
puts string
Output:
12. length
This method used to get the number of elements inside the array. So in the below example, we have one array of students and we wanted to know the number of elements inside the array so we are using length method. The output returns the number which is the length of the array. Follow the example below along with the output screen.
Code:
array= [ "r", "a", "n", "j", "a" ,"n" ]
puts array.length
Output:
13. map
This method is used for the iteration of the given array. We can do any modification into the given array element with the help of the map method. In the below example we are adding an! the symbol with each element of the array. Follow the example below along with the output screen.
Code:
array= [ "r", "a", "n", "j", "a" ,"n" ]
array1 = array.map {|arr| arr + "!"}
puts "#{array1}"
Output:
Conclusion
From this tutorial we learned the methods of Ruby array, we learned each and also we focused on the understanding of the working of each method, we learned that the methods are the predefined function which reduces developer efforts to do some manipulations on the array.
Recommended Articles
We hope that this EDUCBA information on “Ruby Array Methods” was beneficial to you. You can view EDUCBA’s recommended articles for more information.