Home » Ruby programming

Ruby clear() function

Ruby clear() function: Here, we are going to learn about the clear() function with example in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on September 01, 2019

clear() function in Ruby

Sometimes, a situation may occur when you have to remove all the elements of an array. Removing elements of an array can be carried out by facilitating pop() function but that will be quite a lengthy process because first, you have to calculate the length of the array and then proceed with invoking pop() function one by one.

To make the task simpler and less time consuming, Ruby provides you a predefined function in Ruby's library known as a clear() function. the clear() function is used to remove all the elements of the array and it returns the empty array. It does not accept any parameter clear() function works on the single dimensional array as well as on multidimensional arrays. In case of multidimensional arrays, it will only remove arrays as element from the 2D array, it will not remove the content of arrays as elements.

Syntax:

 Array.clear

Now, let us understand the implementation of clear() function in a broader way with the help of examples.

Example 1:

=begin
Ruby program to demonstrate implementation of clear() function
=end

# Initializing some arrays of elements 
Arr1 = ["apple", "mango", "banana", "apricot", "plum", "cherry"] 
Arr2 = ["one","two", "three", "four", "five"] 
Arr3 = [10,20] 
Arr4 = [110, 210, 310, 410, 510] 
Arr5 = ["C++", "Java", "C#", "Visual Basic", "Perl", "Python"]
Arr6 = ["Hrithik", "Satyam", "Amisha", "Kajal", "Wamp"] 

# Invoking clear function 
NewArr1 = Arr1.clear
NewArr2 = Arr2.clear 
NewArr3 = Arr3.clear 
NewArr4 = Arr4.clear 
NewArr5 = Arr5.clear 
NewArr6 = Arr6.clear

# Printing the the corresponding arrays 
puts "#{NewArr1}"
puts "#{NewArr2}"
puts "#{NewArr3}"
puts "#{NewArr4}"
puts "#{NewArr5}"
puts "#{NewArr6}"

Output

[]
[]
[]
[]
[]
[]

Code logic:

You can observe in the above code that when we are invoking the clear() function with the array. The function is returning an array with no elements or it can be termed as an empty array. We are storing that empty array in the corresponding array and printing the arrays with the help of puts function. We are only getting square brackets as the output.

Example 2:

=begin
Ruby program to demonstrate implementation of clear() function
=end

# Initializing some arrays of elements 
Arr1 = ["apple", "mango", "banana", "apricot", "plum", "cherry"] 
Arr2 = ["one","two", "three", "four", "five"] 
Arr3 = [10,20] 
Arr4 = [110, 210, 310, 410, 510] 
Arr5 = ["C++", "Java", "C#", "Visual Basic", "Perl", "Python"]
Arr6 = ["Hrithik", "Satyam", "Amisha", "Kajal", "Wamp"] 
Arr7 = [Arr1, Arr2, Arr3, Arr4, Arr5, Arr6]

# Invoking clear function 
NewArr1 = Arr7.clear

# Printing the  corresponding arrays
puts "#{NewArr1}"

Output

[]

Code logic:

In the above code, we have tried to prove that clear() function also works on multidimensional arrays. We have created a multidimensional array termed as Arr7. We are then proceeding by applying the clear() function on it. clear() function is returning an empty array. It is only removing arrays as elements of the 2D array. It is not affecting the content of Arrays acting as elements.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.