ruby

ruby
# Controller responding with Turbo Stream
class PostsController < ApplicationController
  def create
    @post = Post.new(post_params)

    if @post.save

Hotwire Turbo for SPA-like user experiences

ruby rails hotwire
by Sarah Mitchell 3 tabs
ruby
# spec/factories/users.rb
FactoryBot.define do
  factory :user do
    sequence(:email) { |n| "user#{n}@example.com" }
    name { Faker::Name.name }
    password { 'password123' }

Factory Bot for flexible test data generation

ruby rspec factory-bot
by Sarah Mitchell 1 tab
ruby
# Basic middleware structure
class RequestTimerMiddleware
  def initialize(app)
    @app = app
  end

Rack middleware for request/response processing

ruby rails rack
by Sarah Mitchell 2 tabs
erb
<%# Fragment caching - caches rendered HTML %>
<% cache @product do %>
  <h1><%= @product.name %></h1>
  <p><%= @product.description %></p>
  <p>Price: $<%= @product.price %></p>
<% end %>

Rails caching strategies for performance

ruby rails caching
by Sarah Mitchell 4 tabs
ruby
module Api
  module V1
    class UsersController < ApplicationController
      before_action :authenticate_user!, except: [:index, :show]
      before_action :set_user, only: [:show, :update, :destroy]

RESTful API design with Rails

ruby rails api
by Sarah Mitchell 4 tabs
ruby
class DynamicFinder
  def initialize(records)
    @records = records
  end

  def method_missing(method_name, *args, &block)

Metaprogramming with method_missing and define_method

ruby metaprogramming method_missing
by Sarah Mitchell 3 tabs
javascript
// app/javascript/controllers/dropdown_controller.js
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ["menu"]

Stimulus for sprinkles of JavaScript interactivity

ruby rails stimulus
by Sarah Mitchell 3 tabs
ruby
# Basic exception handling
begin
  result = 10 / 0
rescue ZeroDivisionError => e
  puts "Error: #{e.message}"
  result = nil

Exception handling and error management

ruby exceptions error-handling
by Sarah Mitchell 3 tabs
ruby
# Gemfile
gem 'view_component'

# app/components/button_component.rb
class ButtonComponent < ViewComponent::Base
  VARIANTS = %w[primary secondary danger].freeze

ViewComponent for reusable, testable view components

ruby rails view-component
by Sarah Mitchell 2 tabs
ruby
# Map - transform elements
[1, 2, 3, 4].map { |n| n * 2 }          # => [2, 4, 6, 8]
['alice', 'bob'].map(&:upcase)          # => ['ALICE', 'BOB']
users.map(&:email)                       # => ['alice@example.com', ...]

# Select/Reject - filter elements

Enumerables and collection manipulation

ruby enumerable collections
by Sarah Mitchell 3 tabs