Skip to content
Related Articles

Related Articles

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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"

chevron_right


Output :

clear a : []

clear b : []

Code #2 : Example for clear() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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"

chevron_right


Output :

clear a : []

clear b : []

clear c : []

My Personal Notes arrow_drop_up
Recommended Articles
Page :