Code Explainers
Ruby code explainers
ruby
class EditorHistory MAX_DEPTH = 100 def initialize(initial = "")
How undo/redo history works in Ruby
undo-redo
state-management
immutability
Intermediate
9 steps
ruby
class Current < ActiveSupport::CurrentAttributes attribute :user, :request_id, :user_agent, :ip_address resets { Time.zone = nil }
Per-request audit trails with Current in Rails
thread-local-state
auditing
concerns
Intermediate
8 steps
ruby
class Category < ApplicationRecord belongs_to :parent, class_name: "Category", optional: true has_many :children, -> { order(:position) },
Modeling a self-referential category tree in Rails
self-referential-association
recursion
tree-structure
Intermediate
9 steps
ruby
module WifiQrCode ESCAPE_CHARS = /([\\;,:"])/.freeze AUTH_TYPES = {
Building a Wi-Fi QR code payload in Ruby
string-formatting
validation
escaping
Intermediate
9 steps
ruby
require "nokogiri" require "open-uri" require "set"
Extracting and normalizing links in Ruby
web-scraping
url-parsing
html-parsing
Intermediate
8 steps
ruby
require "yaml" require "date" class Money
Custom YAML serialization in Ruby
serialization
yaml
custom-encoding
Intermediate
7 steps
ruby
module Terminal module Color CODES = { black: 30, red: 31, green: 32, yellow: 33,
Building an ANSI color DSL in Ruby
metaprogramming
monkey-patching
ansi-escapes
Intermediate
9 steps
ruby
class ReportingQuery def initialize(scope = ApplicationRecord) @scope = scope end
Routing heavy reports to a read replica in Rails
read-replica
database-roles
query-object
Intermediate
6 steps
ruby
class WeatherLookup Result = Struct.new(:temperature, :conditions, keyword_init: true) class TimeoutError < StandardError; end
A resilient weather API service object in Rails
service-object
http-client
error-handling
Intermediate
7 steps
ruby
class Post < ApplicationRecord belongs_to :author, counter_cache: false has_many :comments, dependent: :destroy end
A custom counter cache in Rails
counter-cache
callbacks
associations
Intermediate
7 steps
ruby
namespace :active_storage do desc "Purge blobs that have been unattached for more than 48 hours" task purge_stale: :environment do cutoff = 48.hours.ago
A Rake task to purge stale Active Storage blobs in Rails
rake
active-storage
batch-processing
Intermediate
8 steps
ruby
class Article < ApplicationRecord belongs_to :author has_many :taggings, dependent: :destroy has_many :tags, through: :taggings
Keeping a search index fresh in Rails
callbacks
background-jobs
debounce
Intermediate
9 steps
ruby
require "base64" module Auth module BasicCredentials
Parsing HTTP Basic Auth headers in Ruby
parsing
authentication
base64
Intermediate
6 steps
ruby
class WeightedMovingAverageForecaster DEFAULT_WINDOW = 5 def initialize(window: DEFAULT_WINDOW, weights: nil)
A weighted moving average forecaster in Ruby
forecasting
weighted-average
normalization
Intermediate
8 steps
ruby
class ApplicationController < ActionController::Base INACTIVITY_TIMEOUT = 30.minutes before_action :expire_stale_session
Idle session timeout in a Rails controller
authentication
session-management
before-action
Intermediate
6 steps
ruby
module Dotenv LINE = /\A\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*?)\s*\z/ module_function
Parsing a .env file in Ruby
parsing
regular-expressions
environment-variables
Intermediate
8 steps
ruby
class ChatChannel < ApplicationCable::Channel periodically :sweep_stale_typists, every: 3.seconds def subscribed
How a typing indicator works in Rails ActionCable
websockets
presence
cache-expiry
Intermediate
9 steps
ruby
class Author < ApplicationRecord has_many :posts scope :with_post_stats, -> {
Aggregating post stats in a Rails scope
scopes
sql-aggregation
left-join
Intermediate
8 steps