E-Learning

Learn Ruby – The Best Way to Generate a Random String

 
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)

require 'SecureRandom'
SecureRandom.base64
"/2BuBuLf3WfSKyQbRcc/A=="

It generates a string base64. The argument “a” determines the length of the random String created. The length of the String created will be about 4/3 of the value of “a” declared. If “a” is not stated, the number 16 is used as default. The resulting string can contain characters A-Z, a-z, 0-9, ” “, “/” and “=”.
 

Ruby – Creating a random String hex (a)

SecureRandom.hex
"Eb693ec8252cd630102fd0d0fb7c3485"
SecureRandom.hex (7)
"91dc3bfb4de5b1"

It generates a string hexadecimal base. The argument “a” determines the length of the Random String created. The length of the String created will be twice the value of “a” declared. If “a” is not stated, the number 16 is used as default. The resulting String may contain the characters 0-9 and a-f.
 

Ruby – Creating a random binary string using _bytes (a)

SecureRandom.random_bytes
" XD8 xE0   XF4  r  X b2  XFC * WM  xFF  x83  x18  xF45  XB6"

It generates a random binary string. The argument “a” determines the length of the random String that will be created. If “a” is not stated, the number 16 is used. The result can contain any byte between “x00” – “xff”.

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:

 

julemagne

dogweather

Another cool way to find out interesting things about Ruby is to access our project page!

Dr. Michael J. Garbade

I, Dr. Michael J. Garbade is the co-founder of the Education Ecosystem (aka LiveEdu), ex-Amazon, GE, Rebate Networks, Y-combinator. Python, Django, and DevOps Engineer. Serial Entrepreneur. Experienced in raising venture funding. I speak English and German as mother tongues. I have a Masters in Business Administration and Physics, and a Ph.D. in Venture Capital Financing. Currently, I am the Project Lead on the community project -Nationalcoronalvirus Hotline I write subject matter expert technical and business articles in leading blogs like Opensource.com, Dzone.com, Cybrary, Businessinsider, Entrepreneur.com, TechinAsia, Coindesk, and Cointelegraph. I am a frequent speaker and panelist at tech and blockchain conferences around the globe. I serve as a start-up mentor at Axel Springer Accelerator, NY Edtech Accelerator, Seedstars, and Learnlaunch Accelerator. I love hackathons and often serve as a technical judge on hackathon panels.

View Comments

Recent Posts

Highest Stable Coin Yields – (W16 – 2024)

Another week to bring you the top yield platforms for three of the most prominent…

2 weeks ago

LEDU Token OTC Trading

If you hold a large volume of LEDU tokens above 1 million units and wish…

1 month ago

Highest Stable Coin Yields – (W12 – 2024)

It’s another week and like always we have to explore the top yield platforms for…

1 month ago

Binance Auto Invest – the Best Innovation in Crypto since Sliced Bread

At a time where we’re constantly seeking tools and strategies to simplify our crypto investments,…

1 month ago

Highest Stable Coin Yields – March 2024

As we kick off another week, it's time to explore the top yield platforms for…

2 months ago

Education Ecosystem Featured on Business Insider

We're excited to share that Education Ecosystem was recently featured in an article on Business…

2 months ago