Open In App

Ruby | Array clear() operation

Improve
Improve
Like Article
Like
Save
Share
Report

Array#clear() : clear() is an Array class method which removes all the array elements from it.

Syntax:  Array.clear()

Parameter:  Arrays to clear off

Return:  array with all elements cleared

Code #1 : Example for clear() method




# Ruby code for clear() method
   
# declaring array
a = [1, 2, 3, 4]
   
# declaring array
b = [111.11, 2.5, 4.3, 2.224]
  
# clearing the arrays
puts "clear a : #{a.clear()}\n\n"
  
puts "clear b : #{b.clear()}\n\n"


Output :

clear a : []

clear b : []

Code #2 : Example for clear() method




# Ruby code for clear() method
   
# declaring array
a = ["abc", "xyz", "dog"]
   
# declaring array
b = ["cow", "cat", "dog"]
   
# declaring array
c = ["cat", "1", "dog"]
  
# clearing the array
puts "clear a : #{a.clear()}\n\n"
  
puts "clear b : #{b.clear()}\n\n"
  
puts "clear c : #{c.clear()}\n\n"


Output :

clear a : []

clear b : []

clear c : []



Last Updated : 08 Jan, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads