Skip to content

Instantly share code, notes, and snippets.

View pjb3's full-sized avatar

Paul Barry pjb3

View GitHub Profile
@pjb3
pjb3 / terminator.sql
Created March 25, 2015 02:14
Kill all queries that have been running for more than a minute in a Postgres datavase
select pg_terminate_backend(pid)
from (
SELECT
pid
FROM pg_stat_activity
WHERE query <> '<insufficient privilege>'
AND state <> 'idle'
AND pid <> pg_backend_pid()
AND query_start < now() - interval '1 minute'
ORDER BY query_start DESC) t;
@pjb3
pjb3 / georelocate.coffee
Created April 2, 2014 15:28
How to fake your location in a browser
@navigator.geolocation.watchPosition = (callback) ->
setInterval(->
callback
coords:
latitude: 39.286031201326416,
longitude: -76.61204230156727
, 5000)
(defn foldr [f acc coll]
(if (first coll)
(f (first coll) (foldr f acc (rest coll)))
[]))
@pjb3
pjb3 / .irbrc
Last active February 19, 2016 14:13
Basic Ruby IRB config
require 'irb/completion'
# History
#require 'irb/ext/save-history' #wirble does history
IRB.conf[:PROMPT_MODE] = :SIMPLE
IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"
IRB.conf[:AUTO_INDENT] = true
# use HIRB if available
@pjb3
pjb3 / default_values.rb
Created January 14, 2014 16:07
Looking when default arguments are resolved in Ruby 2.0
def foo(x: wtf)
end
puts "wtf did not get called when the method was defined"
foo x: 1
puts "wtf did not get called when we passed a value for x"
begin
@pjb3
pjb3 / confusing.rb
Created January 7, 2014 04:53
This is why people get class_eval and instance_eval confused in Ruby. You create class methods with instance eval and instance methods with class eval.
class Foo
instance_eval <<-end_eval, __FILE__, __LINE__
def bar
"class method"
end
end_eval
class_eval <<-end_eval, __FILE__, __LINE__
def bar
"instance method"
@pjb3
pjb3 / config.ru
Created December 27, 2013 15:33
Why don't Firefox, Chrome or Safari support passing HTTP Basic Auth user/pass in the URL the way cURL does? I am running the rack server with the rackup command. In all of these requests, I am loading the URL http://user:pass@localhost:9292. In the output, you can tell which client is used for each request from the user agent header. I tried cur…
require 'json'
run -> (env) {
body = JSON.pretty_generate(env) + "\n"
puts body
[ 200, { 'Content-Type' => 'application/json',
'Content-Length' => body.length.to_s }, [body] ]
}
@pjb3
pjb3 / bench.rb
Created December 12, 2013 00:45
A benchmark of calling methods in different ways
require "benchmark"
test = "hi man"
m = test.method(:length)
n = 100000
Benchmark.bmbm {|x|
x.report("meth") { n.times { test.length } }
x.report("call") { n.times { m.call } }
x.report("send") { n.times { test.send(:length) } }
x.report("eval") { n.times { eval "test.length" } }
}
@pjb3
pjb3 / irb_output.txt
Created October 1, 2013 00:21
Rails 4 appears to log SQL queries in the console in development by default. If it doesn't, I can't figure out why it is for me.
$ cat ~/.irbrc
cat: /Users/pbarry/.irbrc: No such file or directory
$ rails c
Loading development environment (Rails 4.0.0)
irb(main):001:0> Subscription.first
Subscription Load (2.5ms) SELECT "subscriptions".* FROM "subscriptions" ORDER BY "subscriptions"."id" ASC LIMIT 1
=> nil
irb(main):002:0> puts ENV.keys.sort
Apple_PubSub_Socket_Render
Apple_Ubiquity_Message
@pjb3
pjb3 / wat.html
Last active December 20, 2015 17:19
Why is the inner div underlined?
<html>
<body>
<div style="text-decoration: underline; font-weight: bold">
I am underlined
<div style="text-decoration: none; font-weight: normal">
I am not
</div>
</div>
</body>
</html>