Ruby | clear() function

The clear() function in Ruby is used to remove all the elements of the given array and returns the array with no elements.

Syntax: Array.clear
Here Array is the input array whose elements are to be cleared.

Parameters: This function does not accept any parameter.



Returns: the array with no elements.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Initializing some arrays of elements
Array1 = ["Alphabets", "a", "b", "c", "d", "e"]
Array2 = ["Names", "gfg", "Geeks", "Geek", "GeeksforGeeks"]
Array3 = ["City", "Kolkata", "Mumbai", "Delhi", "Patna"]
  
# Calling clear() function
A = Array1.clear
B = Array2.clear
C = Array3.clear
  
# Printing the cleared array
puts "#{A}"
puts "#{B}"
puts "#{C}"

chevron_right


Output:

[]
[]
[]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Initializing some arrays of elements
Array1 = []
Array2 = [1, 2]
Array3 = ["a", "b", "c"]
  
# Calling clear() function and
# Printing the cleared array
puts "#{Array1.clear}"
puts "#{Array1.clear}"
puts "#{Array1.clear}"

chevron_right


Output:

[]
[]
[]

Reference: https://devdocs.io/ruby~2.5/array#method-i-clear



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.