instance method
Random::Formatter#base64
base64(n=nil)
lib/random/formatter.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/lib/random/formatter.rb, line 114
def base64(n=nil)
[random_bytes(n)].pack("m0")
end
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.