Skip to content

Instantly share code, notes, and snippets.

# Elixir solution for http://www.codewars.com/kata/dont-drink-the-water
defmodule Liquids do
@density %{ ?H => 1.36, ?W => 1.00, ?A => 0.87, ?O => 0.8 }
def separate_liquids([]), do: []
def separate_liquids([h|_] = glass) when is_list(h) and length(h) > 0 do
List.flatten(glass) |> Enum.sort_by(&(@density[&1])) |> Enum.chunk(length(h))
end
end
@bbugh
bbugh / ipython_magic_function_inspector.py
Last active December 17, 2016 00:46
iPython Django extensions
"""
An IPython magic function to pretty-print objects with syntax highlighting.
Updated to also pretty print the object's __dict__ if it's available.
See, "Defining your own magics":
http://ipython.org/ipython-doc/stable/interactive/reference.html#defining-your-own-magics
For more on Pygments:
http://pygments.org/docs/quickstart/
Usage
@bbugh
bbugh / .overcommit.yml
Created June 25, 2017 18:59
Overcommit setup for Rails apps
# Use this file to configure the Overcommit hooks you wish to use. This will
# extend the default configuration defined in:
# https://github.com/brigade/overcommit/blob/master/config/default.yml
#
# At the topmost level of this YAML file is a key representing type of hook
# being run (e.g. pre-commit, commit-msg, etc.). Within each type you can
# customize each hook, such as whether to only run it on certain files (via
# `include`), whether to only display output if it fails (via `quiet`), etc.
#
# For a complete list of hooks, see:
@bbugh
bbugh / spec_helper.rb
Last active September 14, 2017 20:25 — forked from anolson/spec_helper.rb
Profile ActiveRecord queries, output Markdown table of counts
# Originally referenced from https://medium.com/treehouse-engineering/continous-improvements-4741fc3c7daa
RSpec.configure do |config|
config.before(:suite) do
Thread.current[:query_counter] = Hash.new(0)
end
config.around(:example) do |procsy|
callback = lambda do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
@bbugh
bbugh / indifferent_hash_serializer.rb
Created October 6, 2017 14:24
IndifferentHashSerializer for Rails json/jsonb columns
# app/serializers/indifferent_hash_serializer.rb
# Used for converting a json or jsonb column into a useable Rails hash instead
# of the psuedo-json hash that is normally returned by Rails.
class IndifferentHashSerializer
def self.dump(hash)
hash.to_json
end
def self.load(hash)
@bbugh
bbugh / tinymce_spec_helper.rb
Created October 20, 2017 20:34
Improved handling of TinyMCE with error logging and guard checks
module TinyMCESpecHelper
# Fill in a TinyMCE editor with the specified value.
# Pass in the same id you give to TinyMCE, (such as "editorContent")
# *not* TinyMCE's generated one.
#
# NOTE: The first argument is not a css selector, just an element id.
#
# May require selenium chromedriver!
# Adapted from https://gist.github.com/eoinkelly/69be6c27beb0106aa555
#
@bbugh
bbugh / array_inclusion_validator.rb
Last active April 30, 2024 06:20
Rails Postgres Array Column Validator
# app/models/validators/array_inclusion_validator.rb
class ArrayInclusionValidator < ActiveModel::EachValidator
include ActiveModel::Validations::Clusivity
def validate_each(record, attribute, values)
values.each do |value|
unless include?(record, value)
message = (options[:message].try(:gsub, '%{value}', value) || "#{value} is not included in the list")
record.errors.add(attribute, :array_inclusion, message: message, value: value)
end
@bbugh
bbugh / results.txt
Created March 8, 2018 16:04
Benchmarking for checking if one array contains another in Ruby
Warming up --------------------------------------
subtract 233.432k i/100ms # higher is better
bitwise & == 164.352k i/100ms
bitwise & size 218.548k i/100ms
bitwise & [] 200.794k i/100ms
contains_all 74.648k i/100ms
contains_all_count 112.634k i/100ms
set 19.238k i/100ms
set cached 194.675k i/100ms
all? include? 144.048k i/100ms
@bbugh
bbugh / apollo.js
Last active August 11, 2022 07:42
Apollo Action Cable setup
import { ApolloClient } from 'apollo-client'
import { split } from 'apollo-link'
import { HttpLink } from 'apollo-link-http'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { getMainDefinition } from 'apollo-utilities'
import { createPersistedQueryLink } from 'apollo-link-persisted-queries'
import { setContext } from 'apollo-link-context'
// AC: IMPORT ACTIONCABLE
import ActionCable from 'actioncable'
@bbugh
bbugh / _icons.scss
Created September 26, 2018 11:08
iconfont-webpack-plugin setup on webpack 4
// use with <i class="svg-icon bank-icon"></i>
.svg-icon {
vertical-align: middle; // you may not need this line depending on your icons
}
// borrowed from icomoon's font output
.svg-icon::before {
font-weight: normal;
font-style: normal;
font-variant: normal;