Skip to content

Instantly share code, notes, and snippets.


Matt Long mattlong

  • Y Combinator
  • San Francisco, CA
View GitHub Profile
mattlong / Gemfile
Created Jul 13, 2020 — forked from dhh/Gemfile
HEY's Gemfile
View Gemfile
ruby '2.7.1'
gem 'rails', github: 'rails/rails'
gem 'tzinfo-data', '>= 1.2016.7' # Don't rely on OSX/Linux timezone data
# Action Text
gem 'actiontext', github: 'basecamp/actiontext', ref: 'okra'
gem 'okra', github: 'basecamp/okra'
# Drivers
View pihole-whitelist.txt
# []
mattlong / csrf_tokens.rb
Created Apr 6, 2020
Rails CSRF token analyzer
View csrf_tokens.rb
def real_csrf_token(token)
def xor_byte_strings(s1, s2)
s2_bytes = s2.bytes
s1.each_byte.with_index { |c1, i| s2_bytes[i] ^= c1 }
sudo kextunload /System/Library/Extensions/AppleHDA.kext
sudo kextload /System/Library/Extensions/AppleHDA.kext
mattlong / .bashrc
Created Feb 4, 2020
Fix GPG decrypting
View .bashrc
# If you get "Inappropriate ioctl for device" when decrypting
# Thanks to
export GPG_TTY=$(tty)

Rails dirty

After modifying an object and after saving to the database, or within after_save:

Rails <= 5 Rails >= 5.1
attribute_changed? saved_change_to_attribute?
changed? saved_changes?
changes saved_changes
attribute_was attribute_before_last_save
mattlong /
Created Apr 23, 2019
Kill defunct proccesses
parents_of_dead_kids=$(ps -ef | grep [d]efunct | awk '{print $3}' | sort | uniq | egrep -v '^1$'); echo "$parents_of_dead_kids" | xargs kill
mattlong / organization.rb
Last active Nov 15, 2018
Rails / ActiveRecord has_one and has_many to same model
View organization.rb
# From
# Almost good, but I don't think it handles the case of promoting an
# existing location to being the primary location correctly in some cases
class Organization < ActiveRecord::Base
has_many :locations, dependent: :destroy, autosave: true # autosave necessary for the importer
has_one :primary_location, -> { where(locations: { primary: true }) }, class_name: "Location", autosave: true
# Override getter to fix issue with Rails not reloading the primary_location after resetting it to nil
def primary_location
mattlong / rails-jsonb-queries
Created Sep 24, 2018 — forked from mankind/rails-jsonb-queries
Rails-5 postgresql-9.6 jsonb queries
View rails-jsonb-queries
#payload: [{"kind"=>"person"}]
Segment.where("payload @> ?", [{kind: "person"}].to_json)
#data: {"interest"=>["music", "movies", "programming"]}
Segment.where("data @> ?", {"interest": ["music", "movies", "programming"]}.to_json)
Segment.where("data #>> '{interest, 1}' = 'movies' ")
Segment.where("jsonb_array_length(data->'interest') > 1")
mattlong / where_is.rb
Created Aug 27, 2018 — forked from wtaysom/where_is.rb
A little Ruby module for finding the source location where class and methods are defined.
View where_is.rb
module Where
class <<self
attr_accessor :editor
def is_proc(proc)
def is_method(klass, method_name)