Skip to content

Instantly share code, notes, and snippets.

@fledman
fledman / keybase.md
Created July 14, 2014 17:01
keybase

Keybase proof

I hereby claim:

  • I am fledman on github.
  • I am fledman (https://keybase.io/fledman) on keybase.
  • I have a public key whose fingerprint is A35B DDE8 C870 DB7B D663 242B 3FCB 4801 83BE B6F0

To claim this, I am signing this object:

@fledman
fledman / gist:66b37c0a532617bc57e3
Created March 16, 2015 22:27
Mocha Callable Returns
=begin
Extends Mocha to support dynamic return values.
Now you can set the return value of your stub or
expectation to a proc; the proc is evaluated each
time the method is called. For example, you can
now do Foo.stubs(:when).returns(->{Time.now}) and
each call to Foo.when will return the current time.
Note that this behaviour is only available while inside
of a block passed to Mocha.with_callable_returns.
@fledman
fledman / gist:90993b89c6eab55e9059
Created March 16, 2015 22:46
Mocha #with_stubs
=begin
block-form stubbing for Mocha; stubs get cleaned up after the block
each argument to #with_stubs should be an array of the form
[object to stub on, method symbol, value to return]
the stub object can be an instance, a class, or Klass.any_instance
e.g. stub1 = [Foo.any_instance, :bar, bar = mock()]
stub2 = [bar, :count, 5]
with_stubs(stub1,stub2) { assert_equal Customer.foo.bar.count, 5 }
=end
@fledman
fledman / gist:bbc94017a468d240d520
Created May 1, 2015 19:53
Lexicographic permutations
# Project Euler: Problem 24
# Lexicographic permutations
# https://projecteuler.net/problem=24
def fact(x); (1..x).reduce(1,:*); end
l=(0..9).to_a
out = []
marker = 1_000_000
while !l.empty? do
@fledman
fledman / dot_math.rb
Last active August 29, 2015 14:21
Chained Arithmetic
class DotMath
NUMBERS = {
zero: 0, one: 1, two: 2, three: 3, four: 4, five: 5,
six: 6, seven: 7, eight: 8, nine: 9, ten: 10
}.freeze
OPERATORS = {
plus: [:+,1], minus: [:-,1], times: [:*,1], divide: [:/,1],
negate: [:-@,0], abs: [:abs,0], pow: [:**, 1], mod: [:%, 1]
}.freeze
require 'forwardable'
module ADT
class HashCache
extend Forwardable
include Enumerable
attr_reader :max_size, :default_value, :store_default
def initialize(max_size, default: nil, store_default: true)
@fledman
fledman / ruby-switch.sh
Last active July 19, 2019 05:24
joint auto switching for postmodern ruby tools chruby and gem_home (ZSH)
source /usr/local/share/chruby/chruby.sh
source /usr/local/share/gem_home/gem_home.sh
unset RUBY_SWITCH_DEFAULT
unset RUBY_SWITCH_CURRENT
unset RUBY_SWITCH_GEMHOME
RUBY_SWITCH_DEFAULT=$(cat ~/.ruby-switch-default)
if [[ -z "$RUBY_SWITCH_DEFAULT" ]]; then