Skip to content

Instantly share code, notes, and snippets.

View jwilger's full-sized avatar

John Wilger jwilger

View GitHub Profile
@jwilger
jwilger / wtf.rb
Created July 28, 2012 06:32
Sometimes I Hate Ruby
require 'delegate'
class Foo
def select
'Foo#select'
end
end
class Bar < SimpleDelegator
def a
@jwilger
jwilger / translate.rb
Created July 20, 2012 18:49
TDA, no mutation, no (used) return values between objects
class Printer
def initialize(options)
@out = options.fetch(:out)
@translator = options.fetch(:translator)
end
def say_hello
@translator.translate("Hello!", self)
end
@jwilger
jwilger / wishful_thinking.rb
Created June 26, 2012 22:37
I really wish I could do this in Ruby
class X
def connect(action, to, with_success_status_code)
:result_a
end
def connect(different_thing_entirely)
:result_b
end
end
def my_method(options = {})
observer = options.delete(:observer)
x = SomeObservable.new
x.add_observer(*observer)
end
# works with
my_method(:observer => some_object)
# and also with
@jwilger
jwilger / ttt.rb
Created May 10, 2012 01:01
Tic-Tac-Toe in Ruby
#!/usr/bin/env ruby
def winner_is(winner)
puts "*Time passes...*"
if winner == :computer
puts "I win!"
else
puts "OK, you win this one."
end
end
@jwilger
jwilger / my_given_driver.rb
Created February 25, 2012 23:24
TestData's @DaTa is a Hash that defaults to empty Hashes?
class MyGivenDriver < Kookaburra::GivenDriver
def a_bar(name)
bar_data = test_data.default(:bar)
record = api.create_bar(bar_data)
test_data.set_bars(name, record)
end
def a_foo(name, bar_name)
foo_data = test_data.default(:foo)
@jwilger
jwilger / Gemfile
Created November 15, 2011 18:33
Cucumber and therubyracer not getting along :-(
source 'http://rubygems.org'
gem 'rails', '3.1.1'
gem 'pg'
gem 'json'
gem 'therubyracer', :require => 'v8'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.1.4'
@jwilger
jwilger / gist:1175717
Created August 27, 2011 18:39
Yay for #tap
let(:some_thing) { mock_model(SomeThing) }
let(:exception) do
ActiveRecord::RecordInvalid.allocate.tap do |e|
e.stub!(record: some_thing)
end
end
@jwilger
jwilger / install_pow_with_apache_proxy.sh
Created April 20, 2011 22:53
Run this file to instal 37signals' Pow but use an Apache proxy instead of the firewall rule.
curl get.pow.cx/install.sh | sh
# We don't actually want the firewall rule that pow installs
echo "Get rid of those silly firewall rules for port 80."
ports=($(ruby -e'puts $<.read.scan(/fwd .*?,([\d]+).*?dst-port ([\d]+)/)' "/Library/LaunchDaemons/cx.pow.firewall.plist"))
HTTP_PORT=${ports[0]}
DST_PORT=${ports[1]}
RULE=$(sudo ipfw show | (grep ",$HTTP_PORT .* dst-port $DST_PORT in" || true) | cut -f 1 -d " ")
[[ -n "$RULE" ]] && sudo ipfw del "$RULE"
# Unload the firewall plist and remove it.
@jwilger
jwilger / ruby_bug_eh.txt
Created December 5, 2010 01:05
Not sure if this is an interpreter bug or just a misunderstanding on my part.
ree-1.8.7-2010.02 > s.each do |x|
ree-1.8.7-2010.02 > begin
ree-1.8.7-2010.02 > puts '%02d' % x
ree-1.8.7-2010.02 ?> rescue ArgumentError => e
ree-1.8.7-2010.02 ?> puts e
ree-1.8.7-2010.02 ?> end
ree-1.8.7-2010.02 ?> end
01
02
03