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 clear method only works on arrays.

The illustration below shows how the clear method works in Ruby:

Return value

The clear method returns an array with no elementsempty array.

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.clear
puts "Updated Array: #{myArray}"

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved