Skip to content

Instantly share code, notes, and snippets.

@synth
synth / flag_registration.rb
Created February 14, 2024 06:33
Feature flag code detection
module Flipper
module FlagRegistration
# These functions are all memoized because they should be static for the
# lifetime of a deployment (albeit they are really static to a Ruby process)
def self.registered_flags
@registered_flags ||= YAML.load_file("config/feature_flags.yml")
end
def self.flags_in_code
@flags_in_code ||= begin
@navidemad
navidemad / benchmark.rb
Last active October 2, 2023 21:57
Benchmark Rails methods
# frozen_string_literal: true
# Usage:
# bundle exec rails runner benchmark.rb
require "benchmark"
require "benchmark/ips"
require "objspace"
require "timecop"
@jjb
jjb / file.md
Last active April 30, 2024 11:26
Using Jemalloc 5 with Ruby.md

For years, people have been using jemalloc with ruby. There were various benchmarks and discussions. Legend had it that Jemalloc 5 didn't work as well as Jemalloc 3.

Then, one day, hope appeared on the horizon. @wjordan offered a config for Jemalloc 5.

Ubuntu/Debian

FROM ruby:3.1.2-bullseye
RUN apt-get update ; \
@natematykiewicz
natematykiewicz / migrate-sidekiq-redis.rb
Last active February 21, 2024 17:18
Migrate Sidekiq Redis
# A script to migrate Sidekiq's redis to a new server.
# This obviously can work for any redis, but I only handled
# data types that Sidekiq uses.
require 'redis'
old_redis = Redis.new url: 'redis://old-redis:6379'
new_redis = Redis.new url: 'redis://new-redis:6379'
unknowns = []
@bf4
bf4 / README.md
Created June 7, 2022 17:51
warnings and Ruby 3.0

on CI and local

export RUBYOPT="-W:deprecated" # show warnings, deprecations only
@natematykiewicz
natematykiewicz / unroutable_routes.rake
Last active June 9, 2022 19:47
Find routes that will raise a routing error when requested
desc 'Find routes that will raise a routing error when requested'
task unroutable_routes: :environment do
# A lot of this code was taken from how `rake routes` works
# https://github.com/rails/rails/blob/f95c0b7e96eb36bc3efc0c5beffbb9e84ea664e4/railties/lib/rails/commands/routes/routes_command.rb
require 'action_dispatch/routing/inspector'
unroutables = Rails.application.routes.routes.
map { |r| ActionDispatch::Routing::RouteWrapper.new(r) }.
reject { |r| r.internal? || r.engine? || r.path.starts_with?('/rails/') || !r.controller }.
@palkan
palkan / README.md
Created January 24, 2022 13:29
Rails boot time profiling

Add the following to application.rb:

$icallbacks = []
$icallbacks.define_singleton_method(:print) do
  puts sort_by { |(a, b)| -b }.map { |(a, b)| "#{b}\t\t#{a}" }.join("\n")
end

ActiveSupport::Notifications.subscribe("load_config_initializer.railties") do |event|
 $icallbacks << [event.payload[:initializer], event.duration]
@DmitryTsepelev
DmitryTsepelev / change_column_type.rb
Created August 27, 2020 08:46
How to change column type (e.g., int -> bigint) without downtime
ActiveRecord::Migration.remove_foreign_key(:current_table, :foreign_table) # no lock
ActiveRecord::Migration.add_column(:current_table, :column_bigint, :bigint) # no lock
copy_data = lambda do
CurrentTable.where(column_bigint: nil).where.not(column: nil).in_batches do |batch|
batch.update_all("column_bigint = column")
end
end
@jeromedalbert
jeromedalbert / .gitattributes
Last active March 3, 2024 12:18
Automatically resolve Git merge conflicts in Rails schema.rb by picking the most recent date in the conflict (now works with Rails 5 and recent versions of Git). The following files should be in your home ~ directory. Inspired by https://tbaggery.com/2010/10/24/reduce-your-rails-schema-conflicts.html
db/schema.rb merge=railsschema
@vojtad
vojtad / bench-ruby-concat.md
Last active December 10, 2018 19:00
Benchmark: ruby string concatenation

Runtime

@a = 'A', @b = 'B'
Warming up --------------------------------------
s = s + @a; s = s + @b
                        49.000  i/100ms
     s += @a; s+= @b    51.000  i/100ms
s = "#{s}#{@a}"; s = "#{s}#{@b}"
                        50.000  i/100ms
s.concat(@a).concat(@b)