Skip to content

Instantly share code, notes, and snippets.

View hynkle's full-sized avatar

Jonathan Hinkle hynkle

View GitHub Profile
module ActionController
class Base
private
# For Rails (tested on 2.3.8):
# Displays request parameters hash pretty-printed instead of as an unreadable wall of
# text (but only in development environment).
# Toss this into config/initializers .
# Altered from actionpack-2.3.8/lib/action_controller/base.rb .
@hynkle
hynkle / tablerowstepdef.rb
Created April 21, 2011 19:56
step definition for table rows in cucumber w/ capybara
# The overall effect of this is to assert that there exists a row where all of the strings
# specified can be found in one of its tds.
#
# Then I should see "foo" and "bar" within a row
# Then I should see "foo", "bar", and "baz" within a row
Then /^I should see ((?:"(?:[^"]+)"(?: |, | and |, and ))*"(?:[^"]+)") (?:within|in) a row$/ do |list|
cell_xpaths = list.scan(/"(?:[^"])+"/).map{|cell_content| %(td/descendant-or-self::*[text()=#{cell_content}])}
page.should have_xpath %{//tr/#{cell_xpaths.join %(/ancestor::tr[1]/)}}
end
@hynkle
hynkle / .bash_profile
Created June 3, 2011 15:31
git tab completion and prompt in bash
# Source the git-completion script.
# Your location may vary.
# If you don't have it, you can find it at
# https://github.com/git/git/blob/master/contrib/completion/git-completion.bash
source /usr/local/git/contrib/completion/git-completion.bash
# If you want tab completion to work with your shell aliases for git, you'll have to do some extra work.
alias gco='git checkout'
complete -o default -o nospace -F _git_checkout gco
@hynkle
hynkle / gist:1014642
Created June 8, 2011 15:28
annoy your friends (requires OS X and apg)
# Annoy your friends!
# Requires OS X and APG.
# brew install apg or sudo port install apg
while :; do apg | say -v Princess; done
@hynkle
hynkle / gist:1126233
Created August 4, 2011 20:49
Odd ohm-contrib (mis)behavior
# in both 1.8.7 and 1.9.2
h = {}
h['foo'] = 'bar'
h[Ohm::Types::String.new('foo')] # 'bar'
h = {}
h[Ohm::Types::String.new('foo')] = 'bar'
h['foo'] # nil
@hynkle
hynkle / gist:1128345
Created August 5, 2011 19:49
blockify lambda
# square evens; leave odds alone
[1,2,3,4,5,6,7,8].map &->(i) do
if i%2 == 0
return i**2
else
return i
end
end
@hynkle
hynkle / gist:1132666
Created August 8, 2011 20:33
initialization of hash of arrays
h = Hash.new([])
h[:cats] << "Mittens"
h[:dogs] << "Fifi"
# At this point, I would like h to be
# {:cats => ["Mittens"}, :dogs => ["Fifi"]}
# Instead, it's just {}.
# Obviously this problem is easy to solve, but only at increased verbosity.
# It feels like it's something that should be able to be done in three lines.
@hynkle
hynkle / gist:1138876
Created August 11, 2011 03:59
undef_method metaprogramming confusion
class Foo
def foo
puts "foo"
end
end
class Bar < Foo
undef_method :foo
end
class Foo
def self.define_bar
def self.bar
puts "bar was apparently defined!"
end
end
end
Foo.bar # raises NoMethodError
@hynkle
hynkle / gist:1171100
Created August 25, 2011 16:35
annoyotron
# do not judge
while true; do sleep `echo $RANDOM%300+150 | bc`; echo $(head -c `echo $RANDOM%10+1 | bc` /dev/random) > /dev/$(w | sed -n 's/.*\(s[0-9][0-9]*\).*/tty\1/p' | php -r '$a=file("php://stdin");shuffle($a);print $a[0];'); done