Base64 Encoding in Ruby
Ruby provides built-in support for base64 encoding/decoding.
To run the program, save it as base64_encoding.rb
and use the ruby
command:
The string encodes to slightly different values with the standard and URL-safe base64 encoders (trailing +
vs -
) but they both decode to the original string as desired.
In Ruby, the Base64
module is part of the standard library, so we don’t need to install any additional gems. The strict_encode64
and strict_decode64
methods are used for standard base64 encoding and decoding, while urlsafe_encode64
and urlsafe_decode64
are used for URL-safe base64 encoding and decoding.
Ruby’s Base64
module automatically handles the conversion between strings and byte arrays, so we don’t need to explicitly convert between them like in some other languages.