Ruby
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
71: Base64 Encoding and Decoding
I've seen this bug more times than I care to admit, usually during a late-night debugging session where someone is trying to integrate a third-party API. You're following the documentation perfectly, your credentials are correct, but the server keeps returning a 401 Unauthorized. Here is a snippet of the kind of code that causes it:
require 'base64'
user_pass = "admin:secret123"
encoded_creds = Base64.encode64(user_pass)
# Let's simulate sending this in an HTTP header
puts "Authorization: Basic #{encoded_creds}"
# Output:
# Authorization: Basic YWRtaW46c2VjcmV0MTIz
#
At first glance, it looks fine. The string is encoded, and the output looks like standard Base64. But if you actually inspect that encoded_creds variable using p instead of puts, you'll see the culprit:
p encoded_creds
# Output: "YWRtaW46c2VjcmV0MTIz\n"
The Invisible Newline Bug
The issue is that Ruby's Base64.encode64 method follows the RFC 2045 standard, which automatically inserts a newline character every 60 characters, and always adds a newline at the end. While this was great for old email systems that had strict line-length limits, it's a nightmare for modern HTTP headers or JSON payloads. The API server receives that \n as part of the token, fails to decode it, and rejects your request.
Switching to Strict Encoding
When you're working with APIs, tokens, or database keys, you almost always want "strict" encoding. Ruby provides a specific method for this that omits those pesky line breaks. Here is the fix:
require 'base64'
user_pass = "admin:secret123"
# Use strict_encode64 instead of encode64
encoded_creds = Base64.strict_encode64(user_pass)
p encoded_creds
# Output: "YWRtaW46c2VjcmV0MTIz" (No newline!)
By using strict_encode64, you're generating a single continuous string. It's cleaner, it's predictable, and it's what 99% of modern web services actually expect.
Handling the Reverse Process
Decoding is generally more straightforward because the Base64.decode64 method is forgiving; it typically ignores those newlines if they happen to be there. However, for consistency and safety, I recommend using strict_decode64 if you know the input should be a strict string.
encoded = "YWRtaW46c2VjcmV0MTIz"
decoded = Base64.strict_decode64(encoded)
puts decoded # => "admin:secret123"
One thing to keep in mind: Base64 isn't encryption. I can't stress this enough. It's just a way of representing binary data as text. If you're using Base64 to "hide" a password in a client-side script, you aren't securing anything—you're just obfuscating it. Anyone with a browser console can decode a Base64 string in seconds.
When to actually use binary encoding
You'll most often reach for the Base64 module when you need to transmit something that isn't plain text—like a small image file or a compiled PDF—inside a format that only supports text (like JSON). Since binary data can contain bytes that would be interpreted as control characters or end-of-file markers, encoding it into the 64-character ASCII set ensures it arrives at the destination exactly as it left.
📋 Practical Task
Exercise: Basic Auth Header Generator
You are building a helper method for an API client. Your goal is to create a method called generate_auth_header(username, password) that returns a properly formatted HTTP Authorization string.
- The method should take two arguments:
usernameandpassword. - It must combine them into a string with a colon separator (
username:password). - It must encode that string using strict Base64 encoding.
- The final return value should be the string:
"Basic [encoded_string]".
Test your code with: generate_auth_header("ruby_dev", "p@ssword123"). It should return "Basic cnVieV9kZXY6cEBzc3dvcmQxMjM=" without any hidden newline characters.
There are no comments for now.