Approach to generate a random String in Ruby
When you need to generate a random alphanumeric string of a specified length in Ruby, you have a couple of options.
If you are using Ruby version >= 2.5, Ruby, you can simply go with:
SecureRandom.alphanumeric(length)
For older versions, you can utilize a little numeric conversion hack: integer#to_s method accepts an argument representing the base.
For example:
13.to_s(2) # = “1101” in binary
13.to_s(16) # = “d” in hex
How to generate a random String in Ruby?
In this article, we will learn how to generate a random String in Ruby.