Ruby | Set flatten() function Last Updated : 04 Dec, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 false. Example 1: Ruby # Ruby program to illustrate the flatten method # requires the set require "set" s1 = Set[1, 2, 4, 4] # flatten method used s2 = s1.flatten() # Prints s2 puts s2 Output: Set: {1, 2, 4} Example 2: Ruby # Ruby program to illustrate the flatten method # requires the set require "set" s1 = Set[16, 8, 3, 5, 2] # flatten method used s2 = s1.flatten() # Prints s2 puts s2 Output: Set: {16, 8, 3, 5, 2} Comment More infoAdvertise with us Next Article Ruby | clear() function G gopaldave Follow Improve Article Tags : Ruby Ruby-Methods Ruby Set-class Similar Reads 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 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: CPP #Ruby program to illustrate the clear method #req 1 min read Ruby | Hash flatten() function Hash#flatten() is a Hash class method which returns the one-dimensional flattening hash array. Syntax: Hash.flatten() Parameter: Hash values Return: one-dimensional flattening hash array Example #1 : Ruby # Ruby code for Hash.flatten() method # declaring Hash value a = {a:100, b:200} # declaring Has 2 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 Ruby | clear() function The clear() function in Ruby is used to remove all the elements of the given array and returns the array with no elements. Syntax: Array.clear Here Array is the input array whose elements are to be cleared. Parameters: This function does not accept any parameter. Returns: the array with no elements. 1 min read Ruby | Array class flatten() function flatten() is an Array class method which returns flattened array i.e. a 1D array Syntax: Array.flatten() Parameter: Array Return: 1D array Example #1 : Ruby # Ruby code for flatten() method # declaring array a = [[18, 22], [ 33, nil, 5, 6]] # declaring array b = [[[1, 4, 1, 1, 88, 9]]] # flatten put 1 min read Like