Skip to content

Instantly share code, notes, and snippets.

@chrisjpowers
chrisjpowers / gist:3743662
Created September 18, 2012 15:10
JavaScript Syntax Question
// Do you prefer...
if (a) {
doA();
} else if (b) {
doB();
} else {
doC();
}
// or...
@chrisjpowers
chrisjpowers / tangled-spec.js
Created November 19, 2011 15:15
Example of tangled code for refactoring
describe("pressing the done button", function() {
var container, input, button, welcome;
beforeEach(function() {
container = $("div");
input = $("<input>", {"class": "name"});
button = $("<button>", {"class": "done"});
welcome = $("<div>", {"id": "welcome"});
container.append(input, button, welcome);
$("body").append(container);
});
var Conundrum = {
doIt: function() {return "did it"; },
doItCopy: function() {return this.doIt(); }
};
@chrisjpowers
chrisjpowers / saving_without_callbacks.rb
Created July 6, 2011 21:42
Saving without callbacks - any better way?
module SavingWithoutCallbacks
def save_without_callbacks
raise "Only works for saved records" if new_record?
updates = {}
changes.each {|key, tuple| updates[key] = tuple.last}
self.class.update_all(updates, {:id => self.id}, {:limit => 1})
end
end
ActiveRecord::Base.send :include, SavingWithoutCallbacks
@chrisjpowers
chrisjpowers / wacky_undef.rb
Created May 24, 2011 14:16
Ruby undef vs. remove_method
class A
def hello
"hello from A"
end
end
class B
def hello
"hello from B"
end
// Example taken from the CouchDB Pages app (though they used this inside a show page,
// while I'm using it on a list page -- could be part of the problem?).
//
// I get this same error any time that I return an object (rather than just a body string)
// inside a list function.
function(head, req) {
return {
code : 301,
headers : {
@chrisjpowers
chrisjpowers / crazy_chronic.rb
Created November 2, 2010 20:47
Strange Chronic date parsing, at least if today is 11/2/2010
require 'rubygems'
require 'chronic'
# This has historically worked, but today, not so much
Chronic.parse "next Monday 4:00am" #=> nil
# Though it works fine in the afternoon
Chronic.parse "next Monday 4:00pm" #=> Mon Nov 08 16:00:00 -0600 2010
# Doesn't work on Tuesday either
// This function returns a new function that will run
// the given function in the context of the given scope
// ie 'this' will be whatever you pass in as 'scope'
function scoped(fn, scope) {
return function () {
return fn.apply(scope, arguments);
}
}
var Obj = {
module AutoAssetsHelper
# Outputs script and link tags for js/css based on
# the current action, controller and layout. This
# should be preferred over adding assets within a
# view file for specific views/layouts/controllers.
#
# Example: If UsersController#dashboard is requested
# and it uses the 'front' layout, these files will be
# included if they exist:
function do_something(num, callback) {
// arbitrary logic here
num += 1;
// end arbitrary logic
return callback(num); // or you could do callback.call(this, num)
}
do_something(5, function(new_num) {
alert("After complex arbitrary logic, num is " + new_num);
});