Ruby | Set add? function Last Updated : 07 Jan, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report The add? is an inbuilt method in Ruby which adds the given object to the set and returns self. If the object is already in the set, returns nil. Syntax: s1.name.add?(object) Parameters: The function takes the object to be added to the set. Return Value: It returns self if the object is not in the set and is added, it returns nil if it is already in the set. Example 1: CPP #Ruby program to illustrate the add ? method #requires the set require "set" s1 = Set[2, 1] #Enters 4 into it puts s1.add ? (4) #Enters 4 into it #But set has already 4 puts s1.add ? (4) Output: Set: {2, 1, 4} Example 2: CPP #Ruby program to illustrate the add ? method #requires the set require "set" s1 = Set[5] #Enters 5 into it #But set has already 5 puts s1.add ? (5) #Enters 8 into it puts s1.add ? (8) Output: Set: {5, 8} Reference: https://devdocs.io/ruby~2.5/set#method-i-add-3F Comment More infoAdvertise with us Next Article Ruby | Set add? function G gopaldave Follow Improve Article Tags : Ruby Ruby-Methods Ruby Collections Ruby Set-class Similar Reads Ruby | Set delete? function The delete?() is an inbuilt method in Ruby which deletes the given object from the set and returns the self object. In case the object is not present, it returns nil. Syntax: s1.name.delete(object) Parameters: The function takes a mandatory parameter object which is to be deleted. Return Value: It r 1 min read Ruby | Set flatten!() function The flatten!() is an inbuilt method in Ruby replaces the receiver with the result in place. Returns nil if no modifications were made. Syntax: s1.flatten!() Parameters: The function does not takes any parameter. Return Value: It returns the receiver with the result in place, or else returns nil if n 1 min read Ruby | Set flatten() function The flatten() is an inbuilt method in Ruby returns a new set that is a copy of the set, flattening each containing set recursively. Syntax: s1.flatten() Parameters: The function does not takes any parameter. Return Value: It returns a boolean value. It returns true if the set is empty or it returns 1 min read Ruby | Set empty?() function The empty?() is an inbuilt method in Ruby returns true if the set is empty or it returns false. Syntax: s1.empty?() Parameters: The function does not takes any parameter. Return Value: It returns a boolean value. It returns true if the set is empty or it returns false. Example 1: Ruby # Ruby program 1 min read Ruby | Array to_s() function Array#to_s() : to_s() is a Array class method which returns self array. Syntax: Array.to_s() Parameter: Array Return: self array Example #1 : Ruby # Ruby code for to_s() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = [1, 4, 1, 1, 88, 9] # declaring array c = [18, 22, 50, 1 min read Like