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
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
[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
[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
[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
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 |
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
*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 |
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
git push heroku branch_name:master |
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
irb -r path_to_app.rb |
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
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') |
OlderNewer