Ruby | Set add() method Last Updated : 07 Jan, 2020 Comments Improve Suggest changes Like Article Like Report The add is an inbuilt method in Ruby which adds an element to the set. It returns itself after addition. Syntax: s1.name.add(object) Parameters: The function takes the object to be added to the set. Return Value: It adds the object to the set and returns self. Example 1: CPP #Ruby program to illustrate the add method #requires the set require "set" s1 = Set[2, 1] #Prints s1 puts s1 #Enters 4 into it s1.add(4) #Prints s1 puts s1 #Enters 4 into it #But set has already 4 s1.add(4) #Prints s1 puts s1 Output: Set: {2, 1} Set: {2, 1, 4} Set: {2, 1, 4} Example 2: CPP #Ruby program to illustrate the add() method #requires the set require "set" s1 = Set[] #Prints s1 puts s1 #Enters 1 into it s1.add(1) #Enters 2 into it s1.add(2) #Prints s1 puts s1 Output: Set: {} Set: {1, 2} Reference: https://devdocs.io/ruby~2.5/set#method-i-add Comment More infoAdvertise with us Next Article Ruby | Set add() method G gopaldave Follow Improve Article Tags : Ruby Ruby-Methods Ruby Collections Ruby Set-class Similar Reads Ruby | Set + method The + is an inbuilt method in Ruby that returns a set which contain all the elements of both the sets. The common elements in both the sets appears once. The '+' method acts as union operation. Syntax: s1.name + s2.name Parameters: The function does not takes any parameter. Return Value: It returns 1 min read Ruby | Set - method The - is an inbuilt method in Ruby that returns a new set built by duplicating the set, removing every element that appears in the given enumerable object. Syntax: s1.name - s2.name Parameters: The function does not takes any parameter. Return Value: It returns a new set built by duplicating the set 1 min read Ruby | Set << method The << is an inbuilt method in Ruby which adds an element to the set. Syntax: s1.name << (element) Parameters: The function takes an element which is to be added to the set. Return Value: It adds an element to the set. Example 1: CPP #Ruby program to illustrate the << method #requi 1 min read Ruby | Hash to_s method Hash#to_s() is a Hash class method which gives the string representation of the hash. Syntax: Hash.to_s() Parameter: Hash values Return: string representation of the hash. Example #1 : Ruby # Ruby code for Hash.to_s() method # declaring Hash value a = {a:100, b:200} # declaring Hash value b = {a:100 1 min read Ruby | Hash store() method Hash#store() is a Hash class method that returns an add-on value with the key given by the key-value argument. Syntax: Hash.store()Parameter: Hash values key valueReturn: add on value with the key given by the key-value argument. Example #1 : Ruby # Ruby code for Hash.store() method # declaring H 2 min read Like