Skip to content

Instantly share code, notes, and snippets.

@Bodacious
Bodacious / bad_service_2.rb
Created June 3, 2025 19:18
Another bad service object day
class UserSubscriberService
PROMO_DISCOUNT_PERCENT = Rational(85, 100) # 15% OFF
PREMIUM_PLAN_NAME = 'premium_plan'
def self.call(user)
plan = Plan.find_by(name: PREMIUM_PLAN_NAME)
user.build_subscription
user.subscription.plan = plan
user.subscription.amount = plan.amount * PROMO_DISCOUNT_PERCENT
@Bodacious
Bodacious / bad_service.rb
Created March 7, 2025 16:40
An example of a terrible service object
class UserSubscriberService < ServiceBase
def call(user:, subscription_plan_name:, discount_code:, cadence_months: 1)
@user = user
@subscription_plan = SubscriptionPlan.active.where(name: subscription_plan_name).take
@discount = Discount.find_by(code: discount_code)
if @subscription_plan.name == 'free'
@user.subscriptions.create!(
plan: @subscription_plan,
cadence_months: cadence_months,
@Bodacious
Bodacious / main.rb
Last active January 9, 2025 17:38
Example of stubbing dependencies in Ruby unit test
require 'bundler/inline'
gemfile do
source "https://rubygems.org"
gem "money", require: "money"
gem "minitest"
gem "mocha"
end
# Calculate the current lifetime value of a user based on how much content they have generated
@Bodacious
Bodacious / README.md
Created December 25, 2024 15:01
Ruby kata: The Ghost of Christmas Past

The Ghost of Christmas Past 🎅🎄👻

Christmas, this year, falls on a Wednesday!

But on which day did Christmas fall in years past?

Problem Description

Write a Ruby class that returns the correct weekday of Christmas Day for previous years.

@Bodacious
Bodacious / rspec example.rb
Last active May 4, 2024 10:30
Reply to tweet example
describe "PATCH /settings" do
context "as a partner" do
it "behaves like a user without access" do
user = create(:partner, company:)
params = { company: { website: "https://domain.suffix" } }
headers = { "Accept" => "text/vnd.turbo-stream.html" }
patch(settings_path, params: params, headers: headers)
expect(response).to be_unauthorized
end
@Bodacious
Bodacious / Benchmark semver ordering in Postgresql.rb
Last active April 17, 2025 19:47
Benchmarking different SEMVER ordering strategies in PostgreSQL
require 'bundler/inline'
##
# Get dependencies
gemfile do
source 'https://rubygems.org'
gem 'pg'
gem 'activerecord', require: 'active_record'
gem 'benchmark-ips'
gem 'pry'
@Bodacious
Bodacious / Gemfile
Created January 10, 2023 21:10
Demonstrating the anonymous modules when including blocks in scopes
# frozen_string_literal: true
source "https://rubygems.org"
gem "activerecord", github: "bodacious/rails"
gem "sqlite3"
@Bodacious
Bodacious / main.rb
Created June 28, 2022 10:38
Jbuilder named attributes vs `extract!`
require "bundler/inline"
gemfile do
gem "benchmark-ips"
gem "jbuilder"
gem "activesupport", require: ["active_support"]
end
class User
attr_accessor :id, :name, :email, :password, :age, :username
@Bodacious
Bodacious / string.rb
Created September 11, 2020 15:55
Ruby Palindrome String
class String
def palindrome?
clean_string = self.gsub(/[^\w]/, '').downcase
clean_string == clean_string.reverse
end
end
puts "madam".palindrome? # => true
puts "racecar".palindrome? # => true
puts "madam, . ".palindrome? # => true
puts "02/02/2020".palindrome? # => true
@Bodacious
Bodacious / monty_hall.rb
Created May 31, 2018 17:00
Monty Hall Problem demonstrated in Ruby
class Game
attr_accessor :doors
attr_reader :correct_door
def initialize(door_count: )
chosen_door = (1..door_count).to_a.sample
@doors = door_count.times.map.with_index do |d, i|
Door.new(i+1 == chosen_door)
end
@correct_door = @doors.detect(&:correct?)
end