Skip to content

Instantly share code, notes, and snippets.

@Kallin
Kallin / callback_counter.rb
Created February 2, 2024 18:38
count how many callbacks per model in your Rails app
require 'csv'
# ensure all classes have been laoded
Rails.application.eager_load!
def calc_callback_counts
models = ApplicationRecord.descendants
class_callback_counts = {}
CALLBACKS = [
'before_validation',
'after_validation',
'before_save',
'around_save',
'before_create',
'around_create',
'after_create',
'after_save',
'after_commit',
@Kallin
Kallin / colleague_stats.rb
Last active January 3, 2019 15:56
gives an indication of how much someone has reviewed/commented on someone else's PRs
require 'octokit'
require 'pp'
def obtain_comment_stats
# takes about a minute for 1 months of PRs
stats = {}
# sample_output = {
# "kallin" => {
#!/usr/bin/env ruby
current_branch = `git symbolic-ref --short HEAD`.chomp
if current_branch != "master"
if $?.exitstatus == 0
puts "WARNING: You are on branch #{current_branch}, NOT master."
else
puts "WARNING: You are not on a branch"
end
puts
#!/usr/bin/env ruby
current_branch = `git symbolic-ref --short HEAD`.chomp
if current_branch != "master"
if $?.exitstatus == 0
puts "WARNING: You are on branch #{current_branch}, NOT master."
else
puts "WARNING: You are not on a branch"
end
puts
def num_to_digits(num)
digits = []
until num == 0
digits << num % 10
num = num / 10
end
digits
def find_descendants(person)
descendants = []
stack = person[:children]
until stack.empty?
current_node = stack.pop
children = current_node[:children]
if children
children.each do |child|
def find_descendants(person)
descendants = []
children = person[:children]
if children
descendants << children
children.each do |child|
descendants << find_descendants(child)
class Vegetable
attr_accessor [
:rarity
]
def ferment
# let's make some vegetable jam!
end
@Kallin
Kallin / fibber.rb
Created March 22, 2016 21:39
adnan's a fibber
@fibs = {
0 => 0,
1 => 1,
}
def fib(n)
cached_result = @fibs[n]
return cached_result if cached_result
new_result = fib(n-1) + fib(n-2)