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 / 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
[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)
# 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('.') }
@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
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 {
/* 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__];
});
};
@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'
{ scopeName = 'source.js';
comment = 'JavaScript Syntax: version 2.0';
fileTypes = ( 'js', 'htc', 'jsx' );
foldingStartMarker = '^.*\bfunction\s*(\w+\s*)?\([^\)]*\)(\s*\{[^\}]*)?\s*$';
foldingStopMarker = '^\s*\}';
patterns = (
{ name = 'meta.class.js';
comment = 'match stuff like: Sound.prototype = { … } when extending an object';
match = '([a-zA-Z_?.$][\w?.$]*)\.(prototype)\s*(=)\s*';
captures = {
// http://localhost:60210/api/?qid=anvil_songofpain_backtobasics&poll=2&call_id=1273084702020&method=get_results&auth=0D85A28B-BBDA-4F26-96E6-4FAEA0421A0E&jsonp=Playdar.client.handle_results
Playdar.client.handle_results({"qid":"anvil_songofpain_backtobasics","poll_interval":1000,"poll_limit":6,"query":{"artist":"Anvil","album":"Back to Basics","track":"Song Of Pain"},"solved":false,"results":[{"sid":"2C2556F9-FFB2-4BBA-93F1-9A8EEA93B058","artist":"Anvil","track":"Keep It Up","album":"Back To Basics","mimetype":"audio/mpeg","score":0.6666666666666666,"duration":230,"bitrate":234,"size":2097152,"source":"tieg-zaharia"}]});
// http://localhost:60210/api/?qid=anvil_keepitup_backtobasics&poll=1&call_id=1273084700988&method=get_results&auth=0D85A28B-BBDA-4F26-96E6-4FAEA0421A0E&jsonp=Playdar.client.handle_results
Playdar.client.handle_results({"qid":"anvil_keepitup_backtobasics","poll_interval":1000,"poll_limit":6,"query":{"artist":"Anvil","album":"Back to Basics","track":"Keep it Up"},"solved":true,"results":[{
var λ = function(k){var a=[];while(a.length+1<arguments.length){a.push(arguments[a.length+1])};return function(o){return((o[k]&&o[k]instanceof Function)?o[k].apply(this,a):o[k]);}};
var ary = [
{num:1, f:function(a,b,c){console.log("A: " + a + "," + b + "," + c);}},
{num:2, f:function(a,b,c){console.log("B: " + a + "," + b + "," + c);}},
{num:3, f:function(a,b,c){console.log("C: " + a + "," + b + "," + c);}}
];
ary.forEach(λ('f',1,2,3));
# => A: 1,2,3