Base64 in Ruby
How to encode and decode Base64 in Ruby — with copy-paste examples and a live converter to check your output.
Ruby's standard library base64 module provides strict_encode64/decode64, which handle Base64 without inserting line breaks.
Encode to Base64 in Ruby
require "base64"
encoded = Base64.strict_encode64("Hello, World!")Decode Base64 in Ruby
require "base64"
text = Base64.strict_decode64(encoded)Notes & gotchas
Avoid Base64.encode64 for anything except legacy MIME use — it inserts a newline every 60 characters. strict_encode64/strict_decode64 return a single unbroken line. For URL-safe output use Base64.urlsafe_encode64.
Try it live
Base64 in Ruby — FAQ
How do I Base64 encode a string in Ruby?+
Use the code shown above. Avoid Base64.encode64 for anything except legacy MIME use — it inserts a newline every 60 characters. strict_encode64/strict_decode64 return a single unbroken line. For URL-safe output use Base64.urlsafe_encode64.
How do I decode Base64 back to text in Ruby?+
Use the decode snippet above. Base64 decoding is lossless, so you get the exact original bytes back; decode them with UTF-8 to recover text.
Is Base64 encoding the same across programming languages?+
Yes. Base64 is a standard (RFC 4648), so a string encoded in one language decodes correctly in any other. Only the API differs, not the output.
Does Base64 secure my data?+
No. Base64 is an encoding, not encryption — anyone can decode it. Never use it to protect secrets.