Skip to content

Instantly share code, notes, and snippets.

@kerryb
kerryb / dynamic_modules.rb
Created November 9, 2011 11:48
dynamically including a module depending on an instance property
module DynamicSetup
def dynamic_setup_complete?
@dynamic_setup_complete
end
def method_missing name, *args, &block
if dynamic_setup_complete?
super
else
perform_dynamic_setup
@kerryb
kerryb / chunked_average.rb
Created March 9, 2012 15:42
Reduce list to average of chunks of n items
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
chunk_size = 3
chunked_averages = (0...data.length).zip(data).group_by{|a| a.first / chunk_size}.values.map{|a| a.map(&:last).reduce(&:+) / a.size}
@kerryb
kerryb / gist:2028383
Created March 13, 2012 11:56
Rubygems error
$ gem -v
<internal:lib/rubygems/custom_require>:32:in `rescue in require': undefined method `try_activate' for Gem:Module (NoMethodError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems.rb:32:in `<top (required)>'
from <internal:lib/rubygems/custom_require>:29:in `require'
from <internal:lib/rubygems/custom_require>:29:in `require'
from /usr/local/bin/gem:8:in `<main>'
$ ruby -d -rubygems -e 'puts 42'
@kerryb
kerryb / Console output
Created April 29, 2012 19:28
Failing Erlang monitor
Erlang R15B (erts-5.9) [source] [64-bit] [smp:4:4] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.9 (abort with ^G)
1> c(doubler).
{ok,doubler}
2> c(monitor).
{ok,monitor}
3>
3> monitor:start().
new
@kerryb
kerryb / blockchain.txt
Created January 12, 2016 14:18
Blockchain verification
Verifying that +kerryb is my blockchain ID. No, me neither. https://onename.com/kerryb
class RomanNumerals
NUMBERS = {
1000 => "M",
900 => "CM",
500 => "D",
400 => "CD",
100 => "C",
90 => "XC",
50 => "L",
40 => "XL",
@kerryb
kerryb / ytd.bash
Last active February 23, 2017 21:45
Year-to-date running stats
ytd() {
ytd=$(find ~/Dropbox/Apps/tapiriik -name $(date +%Y)-*_Running*.tcx -print0 | xargs -0 -n1 awk '/DistanceMeters/ {a=$0} END {gsub(/\s*<[^>]*>/, "", a); print a}' | paste -sd+ - | sed 's/\(.*\)/0.000621371 * (\1)/' | bc | sed 's/\(\..\).*/\1/')
proj=$(echo "$ytd * 365 / $(date +%j)" | bc)
echo "YTD: $ytd. Projected: $proj"
}
@kerryb
kerryb / child.ex
Last active July 2, 2018 10:53
Supervised task, with timeout
defmodule Child do
def task(parent) do
Process.sleep(:timer.seconds(2))
send(parent, :finished)
end
end
@kerryb
kerryb / README.md
Last active May 19, 2019 19:00
Basic given/when/then macros for ExUnit

README

A spike looking at adding basic given-when-then steps to ExUnit tests.

Features

  • define tests as sequences of calls to given_, when_ and then_ (unfortunately when is a reserved word)
  • match steps by calling defwhen etc with a string matching the one used in the step
  • interpolate values into step descriptions using {braces}
  • placeholder variable names are available as methods on the magic args variable
defmodule MyApp.Repo do
require Logger
alias Ecto.Association.NotLoaded
alias Ecto.{Changeset, Schema}
alias MyApp.Auth.User
...
@doc """
Wrapper round `Ecto.Repo.insert/2`, which logs the fields of the inserted