Skip to content

Instantly share code, notes, and snippets.

View notahat's full-sized avatar

Pete Yandell notahat

View GitHub Profile
# Here's everything I have in my .zshrc to set up my PATH for homebrew and asdf:
export PATH=/usr/bin:/bin:/usr/sbin:/sbin
eval "$(/opt/homebrew/bin/brew shellenv)"
source $HOMEBREW_PREFIX/opt/asdf/libexec/asdf.sh
# The resulting $PATH looks like this:
#

Keybase proof

I hereby claim:

  • I am notahat on github.
  • I am notahat (https://keybase.io/notahat) on keybase.
  • I have a public key ASBqO0GTuXC19c1Vf9IeExNNmrbDfTmw47SxvTu6G-P3BAo

To claim this, I am signing this object:

class PagingEnumerator < Enumerator
def initialize(&block)
super do |yielder|
page = 0
loop do
items = block.call(page)
break if items.empty?
items.each {|item| yielder.yield(item) }
page += 1
@notahat
notahat / valid.rb
Created October 11, 2012 02:50
Testing fail?
# I have a class that delegates functionality to a couple of objects that it
# constructs. I want to test this in isolation. I want to make sure that the
# objects are constructed with the right arguments. I also want to make sure
# that the results from the objects are handled correctly.
#
# I'm finding it hard to structure the code and test in a way that isn't
# cumbersome. What's below works, but it feels like a lot of stubbing and setup
# for something the should be simpler.
#
# Anyone got a better approach for this?
class Dependency
def self.foo
puts "Foo"
end
def self.bar
puts "Bar"
end
end
@notahat
notahat / capybara_support.rb
Created May 24, 2011 01:58
Make RSpec and Capybara give backtraces for controller exceptions
# Put this in spec/support
# Make sure we get stack traces for controller and view exceptions.
ActionController::Base.class_eval do
def rescue_action_without_handler(exception)
raise exception
end
end
@notahat
notahat / factorial.rb
Created November 16, 2010 05:34
Ruby 1.9 lambda calculus fun
alias λ lambda
λ {|f| λ {|x| f[λ {|y| x[x][y] } ] }[λ {|x| f[λ {|y| x[x][y] }] }] }[λ {|f| λ {|n| n == 0 ? 1 : n * f[n-1] } }][6]
@notahat
notahat / kata.rb
Created September 27, 2010 23:26
def kata(digits = [], sum = 0, has_pair = false)
if sum < 15
start = (digits.last || 0) + 1
(start..9).inject([]) do |result, digit|
result +
kata(digits + [digit], sum + digit, has_pair) +
kata(digits + [digit, digit], sum + digit + digit, true)
end
elsif sum == 15 && has_pair
[digits]
@notahat
notahat / kata.rb
Created September 27, 2010 23:06
def kata(digits = [], has_pair = false)
if digits.inject {|sum, digit| sum + digit } == 15
has_pair ? [digits] : []
else
start = (digits.last || 0) + 1
(start..9).inject([]) do |result, digit|
result + kata(digits + [digit], has_pair) + kata(digits + [digit, digit], true)
end
end
end
# This is a replacement for Ruby's backtick operator that avoids shell
# injection attacks.
#
# In web apps that allow file uploading, it's common to run shell commands that
# operate on those files. For example, you might do this:
#
# `convert -resize 100x100 '#{image_filename}'`
#
# The problem is that a carefully crafted filename could cause trouble.