This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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)> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Foo(options) { | |
var privateInstanceVar = options.thing; | |
var publicInstanceVar = options.otherThing; | |
var publicInterface = { | |
publicFunction: publicFunction, | |
publicInstanceVar: publicInstanceVar | |
}; | |
return publicInterface; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Car | |
def initialize(model, engine = ::Engine.new) | |
@model = model | |
@engine = engine | |
end | |
def running | |
engine.running | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'activerecord', '~> 3.2' | |
gem 'activerecord-mysql2-adapter' | |
group :test do | |
gem 'rspec' | |
gem 'pry' | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
NewerOlder