Code Explainers
Ruby code explainers
ruby
require "shellwords" require "open3" module Backup
Building safe shell commands in Ruby
shell-out
subprocess
command-injection
Intermediate
7 steps
ruby
class UserAgentParser BROWSERS = [ [/Edg\/([\d.]+)/, "Edge"], [/OPR\/([\d.]+)/, "Opera"],
Parsing user-agent strings in Ruby
regex
pattern-matching
lookup-tables
Intermediate
8 steps
ruby
class LogAggregator BUCKET_FORMAT = "%Y-%m-%dT%H:%M" def initialize(entries)
Bucketing log entries by the minute in Ruby
aggregation
hashing
enumerable
Intermediate
5 steps
ruby
class WeeklySignupsReport DEFAULT_WEEKS = 12 def initialize(weeks: DEFAULT_WEEKS, source: User.all)
Building a weekly signups report in Rails
service object
aggregation
group by
Intermediate
7 steps
ruby
class ApplicationController < ActionController::Base EXPERIMENTS = { checkout_button_color: %w[control blue green], onboarding_flow: %w[control streamlined]
How A/B test cohorts are assigned in Rails
a-b-testing
cookies
hashing
Intermediate
8 steps
ruby
class SnippetHighlighter CONTEXT_RADIUS = 60 MAX_TERMS = 8
Building search-result snippets in Ruby
regular-expressions
text-processing
search
Intermediate
9 steps
ruby
require "openssl" require "base32" class TOTP
How TOTP one-time codes work in Ruby
hmac
authentication
totp
Intermediate
7 steps
ruby
class Coupon < ApplicationRecord has_many :redemptions, dependent: :destroy validates :code, presence: true, uniqueness: { case_sensitive: false }
Safe coupon redemption in Rails
validation contexts
pessimistic locking
scopes
Advanced
10 steps
ruby
class AdminConstraint def self.matches?(request) new(request).matches? end
How routing constraints gate admin routes in Rails
routing
authorization
constraints
Intermediate
8 steps
ruby
require "openssl" require "base64" module SecureToken
Authenticated encryption with AES-GCM in Ruby
encryption
aes-gcm
authentication
Advanced
8 steps
ruby
require "erb" class TemplateRenderer class MissingVariableError < StandardError; end
A safe ERB template renderer in Ruby
metaprogramming
method_missing
binding
Intermediate
10 steps
ruby
class ReverseGeocodeSignupJob < ApplicationJob queue_as :default retry_on Geocoder::OverQueryLimitError, wait: 5.minutes, attempts: 3
How Rails jobs handle reverse geocoding
background-jobs
error-handling
geocoding
Intermediate
7 steps
ruby
def wrap_text(text, width = 80) raise ArgumentError, "width must be positive" unless width.positive? text.split(/\n/, -1).map do |paragraph|
How greedy text wrapping works in Ruby
string-processing
greedy-algorithm
text-wrapping
Intermediate
7 steps
ruby
class SyncInventoryJob < ApplicationJob queue_as :integrations retry_on Faraday::TimeoutError,
Resilient background jobs with retry_on in Rails
background-jobs
error-handling
retries
Intermediate
8 steps
ruby
class Pluralizer IRREGULARS = { "person" => "people", "child" => "children",
Building an English pluralizer in Ruby
regular-expressions
lookup-tables
string-manipulation
Intermediate
8 steps
ruby
require 'set' class CronSchedule FIELDS = %i[minute hour day month wday].freeze
Parsing and matching cron expressions in Ruby
parsing
time-arithmetic
sets
Intermediate
10 steps
ruby
module MarkdownHelper ALLOWED_TAGS = %w[ p br strong em del a ul ol li blockquote code pre h1 h2 h3 h4 h5 h6 hr img table thead tbody tr th td
Safely rendering Markdown in a Rails helper
sanitization
markdown
security
Intermediate
8 steps
ruby
class ActivityFeed include Enumerable Entry = Struct.new(:record, :type, :occurred_at, keyword_init: true) do
Building a paginated activity feed in Rails
enumerable
pagination
composition
Intermediate
9 steps