Skip to content

Instantly share code, notes, and snippets.

View FestivalBobcats's full-sized avatar

Ben Hundley FestivalBobcats

  • Mission Cloud
  • Fayetteville, AR
View GitHub Profile
def self.volatility(prices)
mean = (prices.inject {|s,n| s+n}) / prices.length
sd = Math.sqrt((prices.inject(0) {|s,n| s + (n-mean)*(n-mean)}) / (prices.length-1))
rsd = sd/mean*100
('%.2f'%rsd)
end
def run(&block)
begin; yield; rescue => e; Error.report(e); end
end
@FestivalBobcats
FestivalBobcats / .profile
Created September 8, 2011 16:00
Quickly open a gem directory with Textmate
gemopen() {
gem_dir=( $(find $GEM_HOME/gems -maxdepth 1 | grep $1) )
for line in ${!gem_dir[*]}; do
echo $(($line + 1)). ${gem_dir[$line]}
done
if [ ${#gem_dir[*]} == 0 ]; then
echo $1 directory not found.
else
echo -e '> \c'
read inpt
@FestivalBobcats
FestivalBobcats / gist:1217912
Created September 14, 2011 21:55
Inspect every method as it's called
module Breadcrumbs
def self.included(mod)
mod.extend ClassMethods
mod.class_variable_set :@@__defined_methods, []
end
module ClassMethods
def method_added(m)
unless (meths = class_variable_get(:@@__defined_methods)).include?(m)
gemopen() {
gem_dir=( $(find $GEM_HOME/gems -maxdepth 1 | grep $1) )
dir_count=${#gem_dir[*]}
if [ $dir_count == 0 ]; then
echo $1 directory not found.
elif [ $dir_count == 1 ]; then
mate ${gem_dir[0]}
else
for line in ${!gem_dir[*]}; do
echo $(($line + 1)). ${gem_dir[$line]}
__quiet() {
$1 2> /dev/null
}
parse_git_branch() {
__quiet 'git branch' | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
count_git_changes() {
count=$(__quiet 'git status -s' | wc -l)
# Orig
def items options = Hash.new
products = true_category.descendant_products.available.visible
if options[:vendor]
products = products.by_vendor options[:vendor]
end
if options[:factor]
@FestivalBobcats
FestivalBobcats / gist:1284489
Created October 13, 2011 15:19
Ruby splatz
> def a(x,y); end
=> nil
> send :a, [1,2]
ArgumentError: wrong number of arguments (1 for 2)
from (irb):1:in `a'
from (irb):2
from /Users/bhundley/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'
> send :a, *[1,2]
=> nil
@FestivalBobcats
FestivalBobcats / gist:1319903
Created October 27, 2011 15:33
Multi image uploader in Backbone.js
(function($){
//
//-------------------------------------- Model : Image
//
Image = Backbone.Model.extend({
defaults: {
'delete' : false
}
});
@FestivalBobcats
FestivalBobcats / gist:1323387
Created October 28, 2011 19:57
Underscore.js implementation of Cartesian Product
function cartesianProductOf(){
return _.reduce(arguments, function(mtrx, vals){
return _.reduce(vals, function(array, val){
return array.concat(
_.map(mtrx, function(row){ return row.concat(val); })
);
}, []);
}, [[]]);
}