Skip to content

Instantly share code, notes, and snippets.

@kerryb
kerryb / dynamic_modules.rb
Created Nov 9, 2011
dynamically including a module depending on an instance property
View dynamic_modules.rb
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 Mar 9, 2012
Reduce list to average of chunks of n items
View chunked_average.rb
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}
View gist:2028383
$ 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 Apr 29, 2012
Failing Erlang monitor
View Console output
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 Jan 12, 2016
Blockchain verification
View blockchain.txt
Verifying that +kerryb is my blockchain ID. No, me neither. https://onename.com/kerryb
View roman_numerals.rb
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 Feb 23, 2017
Year-to-date running stats
View ytd.bash
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 Jul 2, 2018
Supervised task, with timeout
View child.ex
defmodule Child do
def task(parent) do
Process.sleep(:timer.seconds(2))
send(parent, :finished)
end
end
@kerryb
kerryb / blocks.rb
Created Dec 10, 2013
Ruby blocks, procs, map and reduce
View blocks.rb
# A proc is just a block of code that can be called later:
say_hello = -> { 2 + 2 }
say_hello.call # => 4
# Procs can take parameters:
double = ->(x) { x * 2 }
@kerryb
kerryb / README.md
Last active May 19, 2019
Basic given/when/then macros for ExUnit
View README.md

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