Skip to content

Instantly share code, notes, and snippets.

@jeromedalbert
jeromedalbert / .gitattributes
Last active March 14, 2025 14:16
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)
@joshuap
joshuap / redis.rb
Created November 30, 2018 01:31
Disable dangerous Redis commands in Ruby
# config/initializers/redis.rb
require 'redis'
# Disables the `flushdb` and `flushall` commands.
class Redis
module DangerousCommands
def flushdb
raise 'This is EXTREMELY DANGEROUS! If you really want to EMPTY THE ENTIRE DATABASE, do it from `redis-cli`.'
# You could call `super` here if you want to allow access in some circumstances.
end
@mstruve
mstruve / redis.rb
Created November 16, 2018 19:48
Redis Readonly Console
if ENV['REDIS_ACCESS_MODE'] == 'readonly'
class Redis
class Client
WRITE_COMMANDS = ::Rails.cache.data.command.map { |a| a[0] if a[2].include?('write') }.compact.to_set.freeze
def process(commands)
if commands.flatten.any? { |c| WRITE_COMMANDS.include?(c.to_s) }
raise NotImplementedError, "REDIS_ACCESS_MODE is set to 'readonly', disallowing writes"
end
@sebble
sebble / stars.sh
Last active April 24, 2025 10:07
List all starred repositories of a GitHub user.
#!/bin/bash
USER=${1:-sebble}
STARS=$(curl -sI https://api.github.com/users/$USER/starred?per_page=1|egrep '^Link'|egrep -o 'page=[0-9]+'|tail -1|cut -c6-)
PAGES=$((658/100+1))
echo You have $STARS starred repositories.
echo
@briankung
briankung / docker-pry-rails.md
Last active January 2, 2025 05:19
Using pry-rails with Docker
#!/usr/bin/env ruby
#
# Merge multiple git repositories into a new one.
# Users `git filter-branch` to rewrite paths, so that `git log` works seamlessly
# Built from the script at http://dominik.honnef.co/posts/2016/04/merging-git-repositories/
#------------------------------------------------------------------------------
new_repo = '/tmp/new_repo'
base = '[email protected]:customer'
dest_branch = 'master'
@notozeki
notozeki / Makefile
Last active March 22, 2023 16:27
An example Ruby extension written in Crystal
CRYSTAL = crystal
UNAME = "$(shell uname -ms)"
LIBRARY_PATH = $(shell brew --prefix crystal-lang)/embedded/lib
LIBS = -levent -lpcl -lpcre -lgc -lpthread
LDFLAGS = -Wl,-undefined,dynamic_lookup
TARGET = crystal_example_ext.bundle
$(TARGET): crystal_example_ext.o
$(CC) -bundle -L$(LIBRARY_PATH) -o $@ $^ $(LIBS) $(LDFLAGS)
@salmanasiddiqui
salmanasiddiqui / notifications_controller.rb
Last active March 20, 2024 18:58
Right way to use Rails SSE for live notification
class NotificationsController < ApplicationController
def notify_me
# Rails reserve a db connection from connection pool for each request, lets put it back into connection pool.
ActiveRecord::Base.clear_active_connections!
# required header
response.headers['Content-Type'] = 'text/event-stream'
sse = ActionController::Live::SSE.new(response.stream)
@kevinelliott
kevinelliott / osx-10.10-setup.md
Last active March 29, 2025 07:56
Mac OS X 10.10 Yosemite Setup

Mac OS X 10.10 Yosemite

Custom recipe to get OS X 10.10 Yosemite running from scratch, setup applications and developer environment. I use this gist to keep track of the important software and steps required to have a functioning system after a semi-annual fresh install. On average, I reinstall each computer from scratch every 6 months, and I do not perform upgrades between distros.

This keeps the system performing at top speeds, clean of trojans, spyware, and ensures that I maintain good organizational practices for my content and backups. I highly recommend this.

You are encouraged to fork this and modify it to your heart's content to match your own needs.

Install Software