Skip to content

Instantly share code, notes, and snippets.

View pdswan's full-sized avatar

Peter Swan pdswan

View GitHub Profile
@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 / 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 / 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 / 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 / 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)
" copy all this into a vim buffer, save it, then...
" source the file by typing :so %
" Now the vim buffer acts like a specialized application for mastering vim
" There are two queues, Study and Known. Depending how confident you feel
" about the item you are currently learning, you can move it down several
" positions, all the way to the end of the Study queue, or to the Known
" queue.
" type ,, (that's comma comma)
@pdswan
pdswan / js.js
Created December 20, 2013 04:11
function Foo(options) {
var privateInstanceVar = options.thing;
var publicInstanceVar = options.otherThing;
var publicInterface = {
publicFunction: publicFunction,
publicInstanceVar: publicInstanceVar
};
return publicInterface;
@pdswan
pdswan / slide_10.rb
Last active December 31, 2015 04:19 — forked from anonymous/slide_10.rb
class Car
def initialize(model, engine = ::Engine.new)
@model = model
@engine = engine
end
def running
engine.running
end
@pdswan
pdswan / Gemfile
Created September 10, 2013 06:23
Experimentation with making a wrapper for ActiveRecord::Relation that can be used like any other enumerable. Starting with select.
source 'https://rubygems.org'
gem 'activerecord', '~> 3.2'
gem 'activerecord-mysql2-adapter'
group :test do
gem 'rspec'
gem 'pry'
end
var Channels = require('./channels')
, Emitter = require('events').EventEmitter
var repeater = Channels.createRepeating(250, 'hello', 'world')
, emitter = new Emitter()
, emitterTestChannel = Channels.fromEmitter(emitter, 'test')
, mergedChannel = Channels.merge(repeater, emitterTestChannel)
, mappedChannel = Channels.map(function() {
return Array.prototype.join.call(arguments, ' ')
}, repeater)