How to Convert a Ruby Set to an Array?

Convert Ruby Set to Array

In Ruby, you can convert a set to an array by using the Set#to_a method, for example, like so:

set = Set[1, 2, 3, 4, 5]
array = set.to_a

print array #=> [1, 2, 3, 4, 5]
print array.class #=> Array

Please note that the the order of elements is not guaranteed (i.e. it is uncertain) when you convert a set to an array in Ruby.


Hope you found this post useful. It was published . Please show your love and support by sharing this post.