Whenever you think of the best way to do something, there are different opinions on the subject. Best in what terms? Security, speed, flexibility, code simplicity, and so on. We hope you will contribute with suggestions in the comments area below.
To create a random string in Ruby, we will show in this article the SecureRandom module. For it to work properly, the random number generator should be available, if not, this error will happen: NotImplementedError.
Ruby – Creating a random String base64 (a)
1 2 3 |
require 'SecureRandom' SecureRandom.base64 "/2BuBuLf3WfSKyQbRcc/A==" |
Ruby – Creating a random String hex (a)
1 2 3 4 |
SecureRandom.hex "Eb693ec8252cd630102fd0d0fb7c3485" SecureRandom.hex (7) "91dc3bfb4de5b1" |
Ruby – Creating a random binary string using _bytes (a)
1 2 |
SecureRandom.random_bytes " XD8 xE0 XF4 r X b2 XFC * WM xFF x83 x18 xF45 XB6" |
If you want to explore other questions, you can check our collection of educational videos about Ruby. Below are some examples:
You can also subscribe to some channels that broadcasts in Ruby, such as the following:
Another cool way to find out interesting things about Ruby is to access our project page!