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)
lib/base64.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.3.0/lib/base64.rb, line 328
def urlsafe_encode64(bin, padding: true)
str = strict_encode64(bin)
str.chomp!("==") or str.chomp!("=") unless padding
str.tr!("+/", "-_")
str
end
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=="