Skip to content

Instantly share code, notes, and snippets.

View ramfjord's full-sized avatar

Thomas Ramfjord ramfjord

  • Quantcast
  • San Francisco, CA
View GitHub Profile
@ramfjord
ramfjord / gist:6dc9f0185a1894fe9076
Created June 2, 2015 01:04
TypeError nil is not a symbol or string
my_table = "table_name"
conn = ActiveRecord::Base.connection
begin
conn.execute "CREATE TABLE #{my_table} (key INTEGER, val TEXT);"
klass = Class.new(ActiveRecord::Base) do
self.table_name = my_table
primary_key = "key"
default_scope { order("key DESC NULLS LAST") }
module Test
module ClassMethods
attr_accessor :var
# Make everything in ClassMethods a class method on whatever includes this
def self.included(klass)
klass.extend ClassMethods
end
end
end
module Test
module ClassMethods
attr_accessor :var
# Make everything in ClassMethods a class method on whatever includes this
def self.included(klass)
klass.extend ClassMethods
end
end
end
database=> BEGIN;
BEGIN
database=> PREPARE dumb AS SELECT 'dumb';
PREPARE
database=> ROLLBACK;
ROLLBACK
database=> PREPARE dumb AS SELECT 'dumb';
ERROR: prepared statement "dumb" already exists
pg_dump -a -N schema_1 -N schema_2 \
--dbname=<redacted> | \
pg_restore -Fc -j 8 --verbose \
--dbname=<redacted>
@ramfjord
ramfjord / queue.rb
Created May 27, 2016 19:52
Simple Ruby Queue
class MyQ
def initialize
@head = @tail = []
end
def enq(obj)
@tail << obj << [] # must use methods that mutate the array here
@tail = @tail[1]
obj
end
@ramfjord
ramfjord / blah_spec.rb
Last active July 14, 2016 01:23
Rspec varargs argument matching
# Actual test
it "prefixes each of it's locks with :prefix:" do
expect(ZookeeperLocker).to receive(:lock_no_retry)
.with(start_with("hello"))
lock_for_time_range "hello", @window do
# nothing
end
end
@ramfjord
ramfjord / cards.rb
Last active September 7, 2016 22:33
Test strategies for Wayne's card game (description in comments)
# There are 7 cards strictly numbered from 1 to 7. There are 3 individuals: A,
# B, and C. A and B each have 3 of these cards, and C has just the final 1
# card. The problem is this... A wants to convey to B what his 3 cards are
# (with 100% certainty) in such a way that C is *never* 100% certain of them.
# There is no private pre-strategizing talk between A and B (in other words, C
# can know full well the algorithm/process and still not be able to figure out
# A's holdings).
#
# How can A accomplish this information transmission as described above?
#
@ramfjord
ramfjord / rspec_stub_with_block.rb
Created April 12, 2017 22:55
Rspec stub with block
# I can stub a method like this with rspec-mocks
allow(Math).to receive(:double) { |x| x * 2 }
expect(Math.double(3)).to eq(6)
# Is there an equivalent for Mocha?
// is there a more idiomatic way to check if the client is asking for JSON?
func isJson(r *http.Request) bool {
contentType, hasContentType := r.Header["Content-Type"]
return strings.HasSuffix(r.URL.Path, ".json") ||
(hasContentType && (contentType[0] == "application/json"))
}