Skip to content

Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Base64#urlsafe_encode64

urlsafe_encode64(bin, padding: true)

Returns the RFC-4648-compliant Base64-encoding of bin.

Per RFC 4648, the returned string will not contain the URL-unsafe characters + or /, but instead may contain the URL-safe characters - and _; see Encoding Character Set above:

Example
Base64.urlsafe_encode64("\xFB\xEF\xBE") # => "----"
Base64.urlsafe_encode64("\xFF\xFF\xFF") # => "____"

By default, the returned string may have padding; see Padding, above:

Example
Base64.urlsafe_encode64('*') # => "Kg=="

Optionally, you can suppress padding:

Example
Base64.urlsafe_encode64('*', padding: false) # => "Kg"

The returned string will have no newline characters, regardless of its length; see Newlines above:

Example
Base64.urlsafe_encode64('*') # => "Kg=="
Base64.urlsafe_encode64('*' * 46)
# => "KioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKg=="
See also #urlsafe_decode64 #strict_encode64 also in 3.2 · 3.1 · 3.0