This file contains hidden or 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 CouchRest | |
class Logger | |
def initialize(app, db=nil) | |
@app = app | |
@db = db | |
end | |
def call(env) | |
log['started_at'] = Time.now |
This file contains hidden or 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
# inject our logger into CouchRest HTTP abstraction layer | |
module HttpAbstraction | |
def self.get(uri, headers=nil) | |
start_query = Time.now | |
log = {:method => :get, :uri => uri, :headers => headers} | |
response = super(uri, headers=nil) | |
end_query = Time.now | |
log[:duration] = (end_query - start_query) | |
CouchRest::Logger.record(log) |
This file contains hidden or 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 step_std_dev(val) { | |
n += 1 | |
delta = val - mean | |
mean = mean + delta/n | |
M2 = M2 + delta*(val - mean) # This expression uses the new value of mean | |
if (n > 1) { | |
variance_n = M2/n | |
variance = M2/(n - 1) | |
} | |
} |
This file contains hidden or 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 MathFunctions | |
include InlineTest | |
def factorial(n) | |
(1..n).inject(1) { |acc, x| acc * x} | |
end | |
unit_test do | |
assert factorial(6) == 720 | |
assert factorial(5) == 120 |
This file contains hidden or 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
;; Helpful macro function | |
(defun my-macro-query (arg) | |
"Prompt for input using minibuffer during kbd macro execution. | |
With prefix argument, allows you to select what prompt string to use. | |
If the input is non-empty, it is inserted at point." | |
(interactive "P") | |
(let* ((query (lambda () (kbd-macro-query t))) | |
(prompt (if arg (read-from-minibuffer "PROMPT: ") "Input: ")) | |
(input (unwind-protect | |
(progn |
This file contains hidden or 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 Stop | |
module CantTouchThis | |
def self.included(mod) | |
%w[instance_variable_get instance_variable_set].each do |m| | |
send(:protected, m) | |
end | |
eigenclass = class << mod; self; end | |
%w[const_set class_variable_get class_variable_set public_class_method attr attr_reader attr_writer].each do |m| |
This file contains hidden or 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
/* | |
* Fabrizio Calderan, twitter @fcalderan, 2010.11.02 | |
* I had an idea: could Inception movie be explained by a few javascript closures | |
* and variable resolution scope (just for fun)? | |
* | |
* Activate javascript console =) | |
*/ | |
<script> | |
console.group("inception movie"); |
This file contains hidden or 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
main = print (take 1000 hamming) | |
hamming = 1 : map (2*) hamming ~~ map (3*) hamming ~~ map (5*) hamming | |
where | |
xxs@(x:xs) ~~ yys@(y:ys) -- To merge two streams: | |
| x==y = (x : xs~~ys) -- if the heads are common, take that | |
| x<y = (x : xs~~yys) -- otherwise, take the smaller one | |
| x>y = (y : xxs~~ys) -- and proceed to merge the rest | |
(defun n-hammings (twos threes fives tail n out) | |
(if (= n 0) |
This file contains hidden or 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
ls .rvm/gems/ree-1.8.7-2010.02/gems/ | perl -ne 's/(.*)-[.\d]+/$1/; print' | sort | uniq >~/gems | |
...change to a different ruby... | |
cat ~/gems | xargs gem install |
This file contains hidden or 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 Numeric | |
SCALE_TO_WORD = Hash.new do |h, i| | |
" * 10^#{i * 3}" | |
end.merge({ 1 => " thousand", | |
2 => " million", | |
3 => " billion", | |
4 => " trillion" | |
}) | |
# in my unscientific test, no one really found names beyond trillion useful. |