Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Random::Formatter#base64

base64(n=nil)

Generate a random base64 string.

The argument n specifies the length, in bytes, of the random number to be generated. The length of the result string is about 4/3 of n.

If n is not specified or is nil, 16 is assumed. It may be larger in the future.

The result may contain A-Z, a-z, 0-9, “+”, “/” and “=”.

Example
require 'random/formatter'

Random.base64 #=> "/2BuBuLf3+WfSKyQbRcc/A=="
# or
prng = Random.new
prng.base64 #=> "6BbW0pxO0YENxn38HMUbcQ=="

See RFC 3548 for the definition of base64.

See also #alphanumeric #choose #gen_random also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0