Skip to content

Instantly share code, notes, and snippets.

View rbgrouleff's full-sized avatar

Rasmus Bang Grouleff rbgrouleff

View GitHub Profile
@rbgrouleff
rbgrouleff / foreign_keys.rb
Created March 25, 2020 15:18
Finding foreign keys in schema.rb for a rails app
require "pathname"
require "strscan"
require "yaml/store"
schema = Pathname.new("./db/schema.rb")
fks = Hash.new { |hash, key| hash[key] = [] }
create_table = /create_table /
table_name = /"(\w+)"/
@rbgrouleff
rbgrouleff / rails_4_2_transaction_fun.rb
Created September 30, 2019 15:22
Fun with Rails 4.2 and transactions
class A < ActiveRecord::Base
belongs_to :b
end
class B < ActiveRecord::Base
has_many :as
end
b = B.create
B.transaction do
@rbgrouleff
rbgrouleff / Migrate Chrome tabs to Safari.app
Created April 13, 2017 17:15
Migrate Chrome windows and tabs to Safari
-- Inspired by http://veritrope.com/code/export-all-chrome-tabs-to-a-text-file/
set WindowList to {}
tell application "Google Chrome"
activate
repeat with w in windows
set ActiveTabId to (id of (active tab of w))
set UrlList to {}
set ActiveTabIdx to 1

Keybase proof

I hereby claim:

  • I am rbgrouleff on github.
  • I am nerdd (https://keybase.io/nerdd) on keybase.
  • I have a public key whose fingerprint is F538 1ED7 CFB2 CBDC 7457 E09D 0669 F626 B634 BF61

To claim this, I am signing this object:

@rbgrouleff
rbgrouleff / allocations.rb
Created September 26, 2013 09:56
Let's see if this block leaks objects...
module Allocations
def delta_of(*classes, &block)
counts = Hash[classes.map { |cls|
[cls.name.intern, { before: ObjectSpace.each_object(cls).count }]
}]
yield
classes.each do |cls|
counts[cls.name.intern][:after] = ObjectSpace.each_object(cls).count
end
counts
if defined? ActiveRecord
Pry.commands.command "explain" do |query|
query = target.eval "#{query}.to_sql"
ActiveRecord::Base.connection
.execute("EXPLAIN EXTENDED #{query}")
.to_a
.each { |arr| puts arr.inspect }
end
end
@rbgrouleff
rbgrouleff / gist:1074507
Created July 10, 2011 12:38
Fat arrow usage
class Observable
constructor: ->
@observers = {}
bind: (event, callback) ->
@observers[event] ?= []
@observers[event].push callback
trigger: (event, data) =>
for callback in @observers[event]
[color]
branch = auto
diff = auto
interactive = auto
status = auto