Sarah Mitchell

33 code snips · on codesnips 5 months

Senior Ruby Developer with 10+ years of experience crafting elegant, performant Rails applications. Deep expertise in Ruby metaprogramming, Rails internals, and building...

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
ruby
# app/validators/order_validator.rb
class OrderValidator < ActiveModel::Validator
  def validate(record)
    validate_order_total(record)
    validate_items_availability(record)
    validate_shipping_address(record)

Custom validators and validation patterns

ruby rails validations
by Sarah Mitchell 3 tabs
ruby
# Simple mixin
module Loggable
  def log(message)
    puts "[#{Time.now}] #{self.class.name}: #{message}"
  end
end

Module mixins and concerns for code reuse

ruby modules mixins
by Sarah Mitchell 3 tabs
ruby
# BAD: N+1 query problem
@users = User.all
@users.each do |user|
  puts user.posts.count  # Fires query for each user!
end

ActiveRecord query optimization and N+1 prevention

ruby rails activerecord
by Sarah Mitchell 3 tabs
ruby
class Timer
  def self.measure
    start_time = Time.now
    yield if block_given?
    end_time = Time.now
    end_time - start_time

Blocks, Procs, and Lambdas for functional programming

ruby blocks procs
by Sarah Mitchell 3 tabs
ruby
class UserNotificationJob < ApplicationJob
  queue_as :default

  # Retry up to 5 times with exponential backoff
  retry_on StandardError, wait: :exponentially_longer, attempts: 5

Background jobs with Sidekiq and ActiveJob

ruby rails sidekiq
by Sarah Mitchell 3 tabs
ruby
require 'rails_helper'

RSpec.describe User, type: :model do
  subject(:user) { build(:user) }

  describe 'validations' do

Advanced RSpec testing with shared examples

ruby rspec testing
by Sarah Mitchell 3 tabs
ruby
class UserRegistrationService
  class Result
    attr_reader :user, :errors

    def initialize(success:, user: nil, errors: [])
      @success = success

Service objects for business logic encapsulation

ruby rails service-objects
by Sarah Mitchell 2 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