Ruby | Set clear() function

The clear() is an inbuilt method in Ruby which clears all the elements in the set.

Syntax: s1.name.clear()

Parameters: The function does not takes any parameter.

Return Value: It returns self after clearing all the elements in the set.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

#Ruby program to illustrate the clear method
  
#requires the set
require "set"
  
    s1
    = Set[2, 1]
  
#Prints s1
      puts s1
  
#Clears the set and returns self
#which is printed
          puts s1.clear

chevron_right


Output:

Set: {2, 1}
Set: {}

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

#Ruby program to illustrate the clear method
  
#requires the set
require "set"
  
    s1
    = Set[]
  
#Prints s1
      puts s1
  
#Add 10 to it
          s1
      << 10
  
         puts s1
  
#Clears the set and returns self
#which is printed
             puts s1.clear

chevron_right


Output:

Set: {}
Set: {10}
Set: {}

Reference: https://devdocs.io/ruby~2.5/set#method-i-clear

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.