Code Explainers
Ruby code explainers
ruby
require "csv" def parse_transaction_row(line) fields = CSV.parse_line(line, headers: false, skip_blanks: true)
Parsing one CSV transaction row in Ruby
parsing
type-coercion
error-handling
Intermediate
6 steps
ruby
class NewCommentNotifier < ApplicationNotifier deliver_by :database deliver_by :email do |config|
Multi-channel notifications with Noticed in Rails
notifications
delivery-methods
fan-out
Intermediate
6 steps
ruby
class DocumentsController < ApplicationController before_action :authenticate_user! before_action :set_document
Serving secure file downloads in Rails
authorization
signed-urls
active-storage
Intermediate
8 steps
ruby
require "bigdecimal" module Money CENTS = BigDecimal("0.01")
Handling money safely with BigDecimal in Ruby
bigdecimal
rounding
currency
Intermediate
7 steps
ruby
class ApplicationJob < ActiveJob::Base retry_on ActiveRecord::Deadlocked, wait: 5.seconds, attempts: 3 discard_on ActiveJob::DeserializationError
Instrumenting every job in Rails
active-job
structured-logging
retries
Intermediate
6 steps
ruby
class PasswordResetsController < ApplicationController RESET_PURPOSE = :password_reset RESET_EXPIRY = 15.minutes
Password resets with signed global IDs in Rails
authentication
signed-tokens
global-id
Intermediate
9 steps
ruby
require "yaml" require "pathname" class AppConfig
A typed config loader in Ruby
yaml
configuration
recursion
Intermediate
8 steps
ruby
module ProductsHelper def cached_product_row(product) cache [product, current_user.role] do render partial: "products/row", locals: { product: product }
Russian doll caching that busts itself in Rails
fragment-caching
cache-invalidation
associations
Intermediate
4 steps
ruby
def build_pivot_table(sales) pivot = sales.each_with_object(Hash.new { |h, k| h[k] = Hash.new(0.0) }) do |record, table| region = record.fetch(:region) quarter = record.fetch(:quarter)
Building a pivot table in Ruby
aggregation
nested-hashes
default-values
Intermediate
9 steps
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