A set in Ruby is a data structure used to store unique elements. In Ruby, the length of a set is the number of elements a set contains. We use the predefined method length
to determine the size of the set. It was created solely for this purpose.
Set.length
The value returned is an integer. This integer represents the number of elements present in a set.
# require the set Class to use itrequire "set"# create setsLanguages = Set.new(["Ruby", "PHP","JavaScript","Python"])Numbers = Set.newNumbers << 1Numbers << 2Numbers << 3Emptyset = Set.new# print lengthputs Languages.lengthputs Numbers.lengthputs Emptyset.length
set
classlanguages
, Numbers
, Emptyset
)