How to Convert a Ruby Set to an 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.


This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.