To get a random item from a set in Ruby, you can use the sample method on an array created from the set.
In this example,
mySet
containing elements.to_a
method.sample
method on the array to retrieve a random item.require 'set'
mySet = Set.new(['Apple', 'Banana', 'Orange', 'Mango'])
myArray = mySet.to_a
randomItem = myArray.sample
puts 'Random item from set:', randomItem
Random item from set: Mango
In this tutorial, we learned How to get a Random Item in a Set in Ruby language with well detailed examples.