Code Explainers
Ruby code explainers
ruby
class DigestMailer < ApplicationMailer default from: "notifications@example.com" def weekly_digest(user)
Building a weekly digest email in Rails
mailers
active-record
query-scopes
Intermediate
6 steps
ruby
class OrdersController < ApplicationController rescue_from ActiveRecord::RecordNotUnique, with: :handle_duplicate_submission def create
Idempotent order creation in Rails
idempotency
database-constraints
error-handling
Intermediate
8 steps
ruby
class InventoryItem < ApplicationRecord belongs_to :warehouse validates :quantity, numericality: { greater_than_or_equal_to: 0 }
Safe inventory updates in Rails
concurrency
pessimistic-locking
optimistic-locking
Advanced
7 steps
ruby
require "forwardable" class ShoppingCart extend Forwardable
Delegation in Ruby with Forwardable
delegation
composition
enumerable
Intermediate
9 steps
ruby
class PaymentMethod < ApplicationRecord belongs_to :account encrypts :card_number, deterministic: false
How Rails encrypts sensitive payment fields
encryption
validation
scopes
Intermediate
9 steps
ruby
require "zlib" require "stringio" require "base64"
Compress-and-encode payloads in Ruby
compression
encoding
io-streams
Intermediate
8 steps
ruby
class SignupValidator EMAIL_REGEX = /\A[^@\s]+@[^@\s]+\.[^@\s]+\z/ MIN_PASSWORD_LENGTH = 8
Building a plain-Ruby signup validator
validation
regex
default-hash
Intermediate
7 steps
ruby
class QueryBuilder def initialize(table) @table = table @wheres = []
How a chainable SQL query builder works
fluent-interface
method-chaining
builder-pattern
Intermediate
8 steps
ruby
class DurationParser UNIT_SECONDS = { 'w' => 604_800, 'd' => 86_400,
Parsing duration strings into seconds in Ruby
parsing
regular-expressions
validation
Intermediate
7 steps
ruby
class User < ApplicationRecord has_secure_password normalizes :email, with: ->(email) { email.strip.downcase }
Normalizing user input in Rails models
normalization
validation
data integrity
Intermediate
6 steps
ruby
class SemanticVersion include Comparable attr_reader :major, :minor, :patch, :prerelease
Comparable semantic versions in Ruby
comparable
parsing
operator-overloading
Intermediate
8 steps
ruby
class Account < ApplicationRecord has_many :projects, dependent: :destroy_async has_many :memberships, dependent: :destroy_async has_many :invoices, dependent: :restrict_with_error
Safely closing an Account in Rails
associations
callbacks
transactions
Intermediate
9 steps
ruby
require "digest" class UploadDeduplicator CHUNK_SIZE = 64 * 1024
Deduplicating uploads by content hash
hashing
deduplication
streaming-io
Intermediate
7 steps
ruby
require "csv" class Api::V1::ProductImportsController < Api::V1::BaseController MAX_ROWS = 10_000
Bulk CSV imports with upsert_all in Rails
bulk-insert
csv-parsing
upsert
Intermediate
8 steps
ruby
class WeightedSampler def initialize(weights) @entries = weights.to_a @total = @entries.sum { |_, w| w }
Weighted random sampling with binary search
weighted-sampling
binary-search
cumulative-sum
Intermediate
7 steps
ruby
class HealthController < ApplicationController skip_before_action :authenticate_user!, raise: false skip_forgery_protection
Building a health check endpoint in Rails
health-check
monitoring
error-handling
Intermediate
7 steps
ruby
class ContactImporter def initialize(rows) @rows = rows end
Deduplicating contacts with Enumerable#uniq
deduplication
normalization
enumerable
Intermediate
4 steps
ruby
module Instrumentation module Timing def self.[](*method_names) Module.new do
Timing methods with a prepended module in Ruby in Rails
metaprogramming
prepend
instrumentation
Advanced
7 steps