Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Ruby | Array clear() operation

  • Last Updated : 08 Jan, 2020

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 : []

My Personal Notes arrow_drop_up
Recommended Articles
Page :