Skip to content

Instantly share code, notes, and snippets.

View tiegz's full-sized avatar
👋
Working from home

Tieg Zaharia tiegz

👋
Working from home
View GitHub Profile
@tiegz
tiegz / git-line-count.rb
Created May 18, 2010 17:31
A Ruby script that uses grit.rb and raphael.js to visualize your JS LOC per commit over time
# 1) Put this file in your working directory
# 2) run 'sudo gem install active_support grit'
# 3) run 'ruby git-line-count.rb'
# 4) run 'open loc.html'
# 5) yay
require 'rubygems'
require 'active_support'
require 'grit'
require 'open-uri'
/* Generates a memoizable getter. */
Object.prototype.memoize = function(key, func) {
var __key__ = "__" + key + "__";
this.__defineGetter__(key, function() {
if (this[__key__]) return this[__key__];
this[__key__] = func();
return this[__key__];
});
};
ruleset APPID {
meta {
name "Say It (pronunciations)"
description <<
You: SMS a word, Me: Call you with a pronunciation
>>
author "Tieg Zaharia"
logging on
key twilio {
@tiegz
tiegz / gist:724462
Created December 1, 2010 23:42
Array#flatten in Ruby
# Overwrites Array#flatten with a new pure Ruby implementation
class Array
def flatten(depth=nil)
ary = []
self.each do |_|
if _.is_a?(Array) && (depth.nil? || depth > 0)
ary += _.flatten(depth.nil? ? nil : depth - 1)
else
ary << _
end
# This just demonstrates that "Proc.new" will inherit its containing method's proc argument
# if not given an argument.
n = 100_000
class Foo
def self.return_proc_wout_arg(); Proc.new(); end
end
n.times { (Foo.return_proc_wout_arg {|a| print a }).call('.') }
[Thu, 17 Mar 2011 07:39:59 -0700] INFO: Starting Chef Solo Run
/etc/chef-custom/recipes/cookbooks/mysql2/recipes/default.rb:8:in `from_file': undefined method `apps' for #<Mash:0xb77b9924> (NoMethodError)
@tiegz
tiegz / gist:910479
Created April 8, 2011 18:52
acts_as_list fix
# config/initializers/acts_as_list.rb
module ActsAsList::InstanceMethods
private
# change acts_as_list to *only* move a new record to bottom if 'position' value is blank
def add_to_list_bottom_with_check
add_to_list_bottom_without_check if new_record? && self[position_column].blank?
end
alias_method_chain :add_to_list_bottom, :check
end
@tiegz
tiegz / gist:920550
Created April 14, 2011 21:05
a will_paginate extension
module WillPaginate
class Collection
# This is a special case of paginate() for _dubstep_ fans (_hyper-dubstep_ not supported).
# It is different in this regard:
# * The first page should load +first_per_page+ records
# * The rest of the pages should load +more_per_page+ records
#
# NOTE: will_paginate view helpers not really tested with this (eg the '1,2,3...N' thing)
#
def self.dubstep_paginate(finder, page = 1, first_per_page = 15, more_per_page = 15, finder_opts = {})
@tiegz
tiegz / gist:944349
Created April 27, 2011 14:36
Ruby 1.9 Named Regexp Groups
if /(?<hour>\d?\d)\:(?<min>\d\d)(?<suffix>[pa]m)/ =~ "5:03pm"
puts hour
puts min
puts suffix
end
puts a = (2500 * 56) / 10000.0 # => 14.0
puts b = (250 * 5.6) / 100.0 # => 14.0
puts c = 25 * 0.56 # => 14.0
puts a % 1 # => 0 ... Expected
puts b % 1 # => 0 ... Expected
puts c % 1 # => 1.77635683940025e-15 .. Not Expected