Skip to content

Instantly share code, notes, and snippets.

View bjeanes's full-sized avatar
🕊️

Bo Jeanes bjeanes

🕊️
View GitHub Profile
def explicit
return 42
rescue
:rescued
else
:else
end
def implicit
42
class Hash
# Returns a nested intersection of two Hashes.
#
# This draws a parallel to &/intersect on Array and Set.
#
# FIXME: However, those methods are not recursive, so it may make more sense
# to give this another name.
#
# Examples:
#

Postgres does not really fully support unicode the way some users might expect. Unicode (much to some people's chagrin) supports multiple ways of encoding complex characters.

For instance, the character could be:

  • ば (\u306f\u3099)
  • ば (\u3070)

Like-wise, and á are different (\u0061\u0301 and \u00e1).

@bjeanes
bjeanes / no_new_nulls_trigger.sql
Last active June 5, 2020 00:07
Trigger to prevent introducing new NULL values to a list of specified columns. I'm using this because I have an 8mil-row, 100GB (with indexes) table that I can't afford to lock to add a NOT NULL constraint. This allows me to start enforcing data now, and back-fill the values without downtime. Later, when I can schedule downtime, another constrai…
/*
* Prevent INSERTs or UPDATEs that introduce NULLs. Useful for when the
* table is too big to add NOT NULL constraints without downtime that
* can't currently be performed.
*
* Use like:
*
* CREATE TRIGGER <name>
* AFTER INSERT OR UPDATE
* ON <table>
# Chromedriver has this annoying bug where when trying to click a button or
# something which is obscured by another element due to scrolling, it fails to
# click the button or link.
#
# This is a workaround to always scroll the element into view after finding it.
module Capybara
module Chrome
module Finders
def find(*args)
super(*args).tap do |node|
require 'command/result'
require 'command/result/switch'
module Command
def self.included(klass)
klass.extend Command::ClassMethods
end
module ClassMethods
def call(**options, &block)
@bjeanes
bjeanes / uuid64.rb
Last active June 5, 2020 00:09
Compact URL-safe UUID representation. It might almost be worth having a base66 version (there are 66 url-safe chars)
require 'securerandom'
require 'base64'
module UUID64
extend self
def generate
encode SecureRandom.uuid
end
$ ruby generate_cloudfront_ip_regex.rb
/(?-mix:13\.3[2-3]\.[0-9]{1,3}\.[0-9]{1,3})|(?-mix:52\.46\.([0-5]{0,1}[0-9]|6[0-3])\.[0-9]{1,3})|(?-mix:52\.8[4-5]\.[0-9]{1,3}\.[0-9]{1,3})|(?-mix:52\.222\.(12[8-9]|1[3-9][0-9]|2[0-5][0-9])\.[0-9]{1,3})|(?-mix:54\.182\.[0-9]{1,3}\.[0-9]{1,3})|(?-mix:54\.192\.[0-9]{1,3}\.[0-9]{1,3})|(?-mix:54\.230\.[0-9]{1,3}\.[0-9]{1,3})|(?-mix:54\.239\.(12[8-9]|1[3-8][0-9]|19[0-1])\.[0-9]{1,3})|(?-mix:54\.239\.(19[2-9]|2[0-1][0-9]|22[0-3])\.[0-9]{1,3})|(?-mix:54\.240\.(12[8-9]|1[3-8][0-9]|19[0-1])\.[0-9]{1,3})|(?-mix:204\.246\.16[4-7]\.[0-9]{1,3})|(?-mix:204\.246\.(16[8-9]|17[0-1])\.[0-9]{1,3})|(?-mix:204\.246\.17[4-5]\.[0-9]{1,3})|(?-mix:204\.246\.(17[6-9]|18[0-9]|19[0-1])\.[0-9]{1,3})|(?-mix:205\.251\.(19[2-9]|2[0-1][0-9]|22[0-3])\.[0-9]{1,3})|(?-mix:205\.251\.249\.[0-9]{1,3})|(?-mix:205\.251\.25[0-1]\.[0-9]{1,3})|(?-mix:205\.251\.25[2-3]\.[0-9]{1,3})|(?-mix:205\.251\.254\.[0-9]{1,3})|(?-mix:216\.137\.(3[2-9]|[4-5][0-9]|6[0-3])\.[0-9]{1,3})/
require 'ipaddr'
require 'json'
require 'net/https'
require 'uri'
module CloudfrontIPs
IP_LIST = URI.parse('https://ip-ranges.amazonaws.com/ip-ranges.json')
class << self
# For Rails 4, when we have it, this is sufficient for their whitelisting.
@bjeanes
bjeanes / anon.rb
Created August 3, 2016 00:21
Low effort anonymisation script a `rails-erd` generated `.dot` file (which I used to submit a Graphviz bug report)
dot = ARGF.read
matches = Hash.new { |h, k| h[k] = "Anonymized_#{rand 9999}" }
replaced1 = dot.gsub(/\b"?m_([A-Za-z0-9_:]+)"?\b/) { |m| m.sub(%r|\b#{$1}\b|, matches[$1]) }
regexp = Regexp.union(matches.keys.map { |k| Regexp.new(k) })
replaced2 = replaced1.gsub(regexp) { |m| matches[m] }
puts replaced2