What is the clear method in Ruby?
The clear method in Ruby is used to clear all elements of an array and return the empty array.
The
clearmethod only works on arrays.
The illustration below shows how the clear method works in Ruby:
Return value
The clear method returns
Example
The code snippet below shows how we can use the clear method in Ruby:
myArray = [1, 2, 3, 4, 5]puts "Original Array: #{myArray}"myArray.clearputs "Updated Array: #{myArray}"
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved