Skip to content

Instantly share code, notes, and snippets.

View crazymykl's full-sized avatar

Mike MacDonald crazymykl

View GitHub Profile
@crazymykl
crazymykl / application_record.rb
Last active July 24, 2019 15:12
Flagged base class
# This works because the first constant classes are assigned to automagically becomes their main name
ApplicationRecord = if feature_flag?
Class.new(ActiveRecord::Base) do
# implementation
end
else
ActiveRecord::Base
end
@crazymykl
crazymykl / slantrhyme.rb
Last active April 27, 2017 15:14
Find words that sorta rhyme with the input
#! /usr/bin/env ruby
def words
@_words ||= open 'cmudict.dict' do |f|
f.each_line.with_object({}) do |line, acc|
word, *phonemes = line.strip.split ' '
acc[word] = phonemes
end
end
rescue Errno::ENOENT
@crazymykl
crazymykl / index_stats.sql
Created February 28, 2017 15:30
Index usage stats.
SELECT
t.tablename,
indexname,
c.reltuples AS num_rows,
pg_size_pretty(pg_relation_size(quote_ident(t.tablename)::text)) AS table_size,
pg_size_pretty(pg_relation_size(quote_ident(indexrelname)::text)) AS index_size,
CASE WHEN indisunique THEN 'Y'
ELSE 'N'
END AS UNIQUE,
idx_scan AS number_of_scans,
@crazymykl
crazymykl / Main.elm
Last active October 13, 2016 14:43
SSCCE for elm-lang/http#4
module Main exposing (..)
import Html exposing (program, text, Html)
import Json.Decode exposing (succeed)
import Http
type alias Model =
Int
@crazymykl
crazymykl / set2d.elm
Created May 14, 2016 20:48
Repetition.
import Array exposing (Array)
type alias Model = Array (Array Color)
type alias Point = (Int, Int)
type Color = Red | Green | Blue | Purple
set2d : Point -> Color -> Model -> Model
set2d (x, y) color model =
Array.get y model
|> Maybe.map (flip (Array.set y << Array.set x color) model)
@crazymykl
crazymykl / profile_me.rb
Last active March 1, 2017 20:41
Ruby-prof wrapper script
def profile(**options)
fail "U WOT M8" unless block_given?
result = RubyProf.profile { yield }
Tempfile.open %w[vmm .cachegrind] do |tmp|
RubyProf::CallTreePrinter.new(result).print(tmp, **options)
spawn 'qcachegrind', tmp.path
end
@crazymykl
crazymykl / Elevator Hax.js
Last active March 2, 2016 21:43
"Lateral thinking" elevator saga solution
{
init: function(elevators, floors) {
_.random = function () { return 1; };
Math.random = function () { return 1; };
elevators.forEach(function (elevator) {
elevator.on("idle", function() {
elevator.goToFloor(0);
elevator.goToFloor(1);
@crazymykl
crazymykl / count_queries.rb
Created February 9, 2016 15:32
A way to count the number of queries the passed block causes.
def count_queries(&blk)
x = 0
counter = -> (*args) { x += 1 }
ActiveSupport::Notifications.subscribed counter, 'sql.active_record', &blk
x
end
DIGIT_VALUES = {
'I' => 1,
'IV' => 4,
'V' => 5,
'IX' => 9,
'X' => 10,
'XL' => 40,
'L' => 50,
'XC' => 90,
'C' => 100,
- 100.times do
%p/