To create a Set of Sets in Ruby, you can use the `Set` class from the `set` library, which allows you to store unique elements, including other sets.
✐ Examples
1Set of Sets with Integers
In this example,
First, we need to require the `set` library using the `require 'set'` statement. This library provides the `Set` class.
We then create a few individual sets. For example, we create a set named `set1` containing integers `1` and `2` by using `Set.new([1, 2])`.
Similarly, we create another set named `set2` containing integers `3` and `4` by using `Set.new([3, 4])`.
Next, we create the main set named `set_of_sets` that will contain the individual sets. We use `Set.new` again and pass an array of the sets we created earlier: `Set.new([set1, set2])`.
This creates a set of sets where `set1` and `set2` are the elements of the `set_of_sets`.
Finally, we print the `set_of_sets` using the `puts` statement to see the output.
Ruby Program
require 'set'
# Creating individual sets
set1 = Set.new([1, 2])
set2 = Set.new([3, 4])
# Creating a set of sets
set_of_sets = Set.new([set1, set2])
# Printing the set of sets
puts "Set of Sets: #{set_of_sets}"
Output
Set of Sets: #<Set: {#<Set: {1, 2}>, #<Set: {3, 4}>}>
2Set of Sets with Strings
In this example,
First, ensure the `set` library is included in your program by using the `require 'set'` statement.
Create individual sets containing strings. For example, create a set named `set1` with strings `"apple"` and `"banana"` using `Set.new(['apple', 'banana'])`.
Create another set named `set2` with strings `"carrot"` and `"date"` using `Set.new(['carrot', 'date'])`.
Create the main set named `set_of_sets` to contain the individual sets. Use `Set.new` and pass an array of the sets: `Set.new([set1, set2])`.
The resulting `set_of_sets` contains `set1` and `set2` as its elements.
Print the `set_of_sets` using `puts` to display the contents.
Ruby Program
require 'set'
# Creating individual sets
set1 = Set.new(['apple', 'banana'])
set2 = Set.new(['carrot', 'date'])
# Creating a set of sets
set_of_sets = Set.new([set1, set2])
# Printing the set of sets
puts "Set of Sets: #{set_of_sets}"
Output
Set of Sets: #<Set: {#<Set: {"apple", "banana"}>, #<Set: {"carrot", "date"}>}>
3Set of Sets with Mixed Data Types
In this example,
First, include the `set` library by using the `require 'set'` statement to access the `Set` class.
Create individual sets with mixed data types. For example, create a set named `set1` containing an integer `1` and a string `"apple"` using `Set.new([1, 'apple'])`.
Create another set named `set2` containing a float `3.14` and a symbol `:banana` using `Set.new([3.14, :banana])`.
Create the main set named `set_of_sets` to hold the individual sets. Use `Set.new` and pass an array of the sets: `Set.new([set1, set2])`.
The `set_of_sets` will now contain `set1` and `set2` as its elements.
Print the `set_of_sets` using the `puts` statement to see the output.
Ruby Program
require 'set'
# Creating individual sets
set1 = Set.new([1, 'apple'])
set2 = Set.new([3.14, :banana])
# Creating a set of sets
set_of_sets = Set.new([set1, set2])
# Printing the set of sets
puts "Set of Sets: #{set_of_sets}"
Output
Set of Sets: #<Set: {#<Set: {1, "apple"}>, #<Set: {3.14, :banana}>}>
Summary
In this tutorial, we learned How to create a Set of Sets in Ruby language with well detailed examples.