Skip to content

Instantly share code, notes, and snippets.

View pdswan's full-sized avatar

Peter Swan pdswan

View GitHub Profile
@pdswan
pdswan / WaysToChange.hs
Last active August 29, 2015 14:05
An illustration of the beauty of recursion and induction
waysToChange :: Int -> [Int] -> Int
waysToChange 0 _ = 1
waysToChange _ [] = 0
waysToChange amount denominations@(denomination:xs)
| amount < 0 = 0
| otherwise =
(waysToChange (amount - denomination) denominations) +
(waysToChange amount xs)
@pdswan
pdswan / activerecord3_union.rb
Last active August 29, 2015 14:13
An Unsanctioned Union
module ActiveRecordScopeExtensions
def self.union(left, right)
active_record_klass = left.klass
arel_union = [left, right].map do |scope|
scope.select("#{active_record_klass.table_name}.id")
end.reduce(&:union)
temporary_table_name = TemporaryTableName.new([active_record_klass.table_name, "union"].join("_")).to_s
active_record_klass.joins("INNER JOIN (#{arel_union.to_sql}) as #{temporary_table_name} ON #{temporary_table_name}.id = #{active_record_klass.table_name}.id")
@pdswan
pdswan / activerecord3_result.rb
Last active August 29, 2015 14:13
Union SQL Results in ActiveRecord 3 and ActiveRecord 4
[3] pry(main)> ActiveRecordScopeExtensions.union(User.where(name: "Bob"), User.where(name: "Alice")).to_sql
=> "SELECT \"users\".* FROM \"users\" INNER JOIN (( SELECT users.id FROM \"users\" WHERE \"users\".\"name\" = 'Bob' UNION SELECT users.id FROM \"users\" WHERE \"users\".\"name\" = 'Alice' )) as users_union_b96cf9af7d77c501088feac76f259ba6 ON users_union_b96cf9af7d77c501088feac76f259ba6.id = users.id"
@pdswan
pdswan / finding_union.rb
Created January 20, 2015 03:19
The search for #union
[6] pry(main)> left = User.where(name: "Bill")
User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."name" = $1 [["name", "Bill"]]
=> []
[7] pry(main)> $ left.union
Error: Couldn't locate a definition for left.union!
[8] pry(main)> left.respond_to?(:union)
=> true
[9] pry(main)> $ left.method_missing
From: /Users/pdswan/code/scratch/rails4_union/rails4/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.0/lib/active_record/relation/delegation.rb @ line 91:
@pdswan
pdswan / to_sql.rb
Created January 20, 2015 03:59
#to_sql or not #to_sql
[14] pry(main)> left.arel.to_sql
=> "SELECT \"users\".* FROM \"users\" WHERE \"users\".\"name\" = $1"
[15] pry(main)> left.to_sql
=> "SELECT \"users\".* FROM \"users\" WHERE \"users\".\"name\" = 'Bill'"
[16] pry(main)>
@pdswan
pdswan / .watchr
Created March 22, 2011 01:18
Basic Watchr Rules and Actions
def run_spec(file)
unless File.exist?(file)
puts "#{file} does not exist"
return
end
puts "Running #{file}"
system "bundle exec rspec #{file}"
puts
end
@pdswan
pdswan / headers.txt
Created January 4, 2012 03:24
Open Congress Error
*Response Headers*
Date Wed, 04 Jan 2012 03:14:33 GMT
Server Apache/2.2.3 (CentOS)
X-Powered-By Phusion Passenger (mod_rails/mod_rack) 3.0.2
X-Runtime 0.976903
Status 500
Vary Accept-Encoding,User-Agent
Content-Encoding gzip
Content-Length 1267
Content-Type text/html; charset=UTF-8
@pdswan
pdswan / gist:2475468
Created April 24, 2012 01:50
Deploy branch to heroku
git push heroku branch_name:master
@pdswan
pdswan / gist:2475472
Created April 24, 2012 01:51
Console for Sinatra
irb -r path_to_app.rb
@pdswan
pdswan / mailing_list_updater_unit_test.rb
Created June 1, 2012 18:08
Testing Classes for Active Record Callbacks
require 'test/unit'
require 'mocha'
# assumes MailingListUpdater is defined / exposed at the top level. this is not the case
# for the code in this gist.
class MailingListUpdaterTest < Test::Unit::TestCase
test "#update_mailing_list calls enqueue_for_mailing_list_update when a user is present" do
user = mock('User', :enqueue_for_mailing_list_update => true)
record = stub('Record')