Skip to content

Instantly share code, notes, and snippets.

🤠
It's great in Phoenix 🚲

Dave Tapley dukedave

🤠
It's great in Phoenix 🚲
Block or report user

Report or block dukedave

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View second.rb
[4] pry(main)> User.all.second
User Load (42.5ms) SELECT "users".* FROM "users" WHERE "users"."deleted_at" IS NULL
=> #<User id: 2, ...>
[5] pry(main)> User.all.offset(1).limit(1)
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."deleted_at" IS NULL LIMIT 1 OFFSET 1
=> [#<User id: 2, ...]
@dukedave
dukedave / json_stringify.json
Last active Aug 29, 2015
Access array show in object by util.inspect, but not JSON.stringify
View json_stringify.json
{
name: "error",
length: 97,
severity: "ERROR",
code: "42P01",
position: "13",
file: "parse_relation.c",
line: "873",
routine: "parserOpenTable"
}
@dukedave
dukedave / ab.sh
Created Apr 17, 2014
Failing to call res.send(200) in an express app
View ab.sh
$ ab -n 1000 -c 1000 http://localhost:3000/hang
This is ApacheBench, Version 2.3 <$Revision: 1554214 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
apr_socket_recv: Connection reset by peer (104)
View timezone.sql
test=# CREATE TABLE tzs(with_tz TIMESTAMP WITH TIME ZONE, without_tz TIMESTAMP WITHOUT TIME ZONE, comment TEXT);
CREATE TABLE
test=# INSERT INTO tzs VALUES( now(), now(), 'now()');
test=# INSERT INTO tzs VALUES( timezone('America/New_York', now()), timezone('America/New_York', now()), 'timezone(New York, now())');
INSERT 0 1
test=# INSERT INTO tzs VALUES( '2014-05-06T17:14:00Z', '2014-05-06T17:14:00Z', 'ISO 8601 Zulu' );
INSERT 0 1
test=# INSERT INTO tzs VALUES( '2014-05-06T17:14:00-04:00', '2014-05-06T17:14:00-04:00', 'ISO 8601 offset' );
INSERT 0 1
test=# SELECT * FROM tzs ;
@dukedave
dukedave / array_iter.rb
Created Jun 12, 2014
Array Iter in Ruby
View array_iter.rb
class ArrayIter < Array
class Iter < SimpleDelegator
attr_reader :next
attr_reader :prev
end
def <<(other)
last.instance_variable_set(:@next, other) if last
other_iter = Iter.new other
View clustering.txt
Start with a list of clusters:
a, b, x, c, x, x, d, e, f, g, x, h
Select out clusters to try and find a place for:
a, b, c, d, e, f, g, h
Map over a method to (optionally) assign a place (pN) to each cluster:
(this is done with a DelegateClass containing the place or nil)
@dukedave
dukedave / considating_ranges.md
Last active Aug 29, 2015
Consolidating ranges across a union in Postgresql
View considating_ranges.md

Two tables share a user_id, a tstzrange called period, then each have another column called category and place_id respectively. Both have constraints allowing a user to only have one 'other column' value at any time:

 user_id    | integer                  | not null
 period     | tstzrange                | not null
 category   | character varying(16)    |

    "category_events_user_id_period_excl" EXCLUDE USING gist (user_id WITH =, period WITH &&)
@dukedave
dukedave / pg.sql
Last active Aug 29, 2015
Postgres range empty check, with inclusive/exclusive end bound
View pg.sql
test=# SELECT isempty('[0,0]' :: int4range);
isempty
---------
f
(1 row)
test=# SELECT isempty('[0,0)' :: int4range);
isempty
---------
t
@dukedave
dukedave / sequel_pk_in_module.rb
Created Jul 22, 2014
Explicitly declaring Sequel PK in module
View sequel_pk_in_module.rb
def self.included(o)
o.extend ClassMethods
o.set_primary_key [:user_id, :period]
o.unrestrict_primary_key
end
@dukedave
dukedave / indexes.sql
Created Jul 22, 2014
Implicit primary key index is redundant
View indexes.sql
"events_pkey" PRIMARY KEY, btree (user_id, period)
"events_user_id_period_excl" EXCLUDE USING gist (user_id WITH =, period WITH &&)
You can’t perform that action at this time.