Code Explainers

Ruby code explainers

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
ruby
class BulkImporter
  BATCH_SIZE = 500
 
  def initialize(records)

Batching bulk inserts in Rails

batching bulk-insert deduplication
Intermediate 5 steps
ruby
class Credentials
  SENSITIVE = %i[password api_key secret_token access_key].freeze
  REDACTED = "[REDACTED]".freeze
 

Redacting secrets in Ruby object output

serialization introspection security
Intermediate 7 steps
ruby
module Multitenant
  extend ActiveSupport::Concern
 
  included do

How a multitenant concern scopes Rails models

multitenancy default-scope concern
Advanced 8 steps
ruby
module Api
  module V1
    class BaseController < ActionController::API
      rescue_from StandardError, with: :render_internal_error

Centralized API error handling in Rails

error-handling rescue-from json-api
Intermediate 7 steps
ruby
require "openssl"
require "base64"
 
module WebhookSignature

Signing and verifying webhooks in Ruby

hmac cryptography webhooks
Intermediate 7 steps
ruby
class CartMergeService
  def initialize(guest_cart:, user_cart:)
    @guest_cart = guest_cart
    @user_cart = user_cart

Merging a guest cart into a user cart in Rails

service object transactions idempotency
Intermediate 7 steps
ruby
require 'monitor'
 
class TokenBucket
  include MonitorMixin

A thread-safe token bucket rate limiter in Ruby

rate-limiting concurrency thread-safety
Intermediate 8 steps
ruby
class Product < ApplicationRecord
  belongs_to :account
 
  after_save :record_audit_trail

Building an audit trail with Rails callbacks

callbacks audit-logging dirty-tracking
Intermediate 7 steps
ruby
class User < ApplicationRecord
  store_accessor :preferences,
    :theme,
    :timezone,

Storing user settings in a JSON column with Rails

store_accessor json-columns validations
Intermediate 7 steps