Skip to content

Instantly share code, notes, and snippets.

@kevinpang
kevinpang / gist:936933
Created April 22, 2011 15:58
Initial tagging implementation
def Post
attr_accessor :tag_names
has_and_belongs_to_many :tags
after_save :update_tags
private
def update_tags
tags.clear
@kevinpang
kevinpang / gist:1015589
Created June 8, 2011 22:24
JavaScript module pattern template
var myNamespace = (function(){
// Private variables / methods
var myPrivateVar = 0;
var myPrivateMethod = function(someText){
console.log(someText);
}
// Public variables / methods
return {
myPublicVar: "foo",
@kevinpang
kevinpang / gist:1015599
Created June 8, 2011 22:30
JavaScript prototypal inheritance
function Shape(x, y) {
this.x = x;
this.y = y;
}
Shape.prototype.toString = function() {
return 'Shape at ' + this.x + ', ' + this.y;
};
function Circle(x, y, r) {
@kevinpang
kevinpang / gist:1015628
Created June 8, 2011 22:40
JavaScript functional inheritance
function Shape(x, y) {
var that = this;
this.x = x;
this.y = y;
this.toString = function() {
return 'Shape at ' + that.x + ', ' + that.y;
};
}
@kevinpang
kevinpang / gist:1088899
Created July 18, 2011 08:31
Ruby Koans
# Greed is a dice game where you roll up to five dice to accumulate
# points. The following "score" function will be used to calculate the
# score of a single roll of the dice.
#
# A greed roll is scored as follows:
#
# * A set of three ones is 1000 points
#
# * A set of three numbers (other than ones) is worth 100 times the
# number. (e.g. three fives is 500 points).
@kevinpang
kevinpang / gist:1088913
Created July 18, 2011 08:38
Ruby Koans 2
# Greed is a dice game where you roll up to five dice to accumulate
# points. The following "score" function will be used to calculate the
# score of a single roll of the dice.
#
# A greed roll is scored as follows:
#
# * A set of three ones is 1000 points
#
# * A set of three numbers (other than ones) is worth 100 times the
# number. (e.g. three fives is 500 points).
@kevinpang
kevinpang / gist:1914606
Created February 26, 2012 06:59
Bad commenting
r = n / 2; // Set r to n divided by 2
// Loop while r - (n/r) is greater than t
while (abs( r - (n/r) ) > t) {
r = 0.5 * ( r + (n/r) ); // Set r to half of r + (n/r)
}
@kevinpang
kevinpang / gist:1914619
Created February 26, 2012 07:05
Better commenting
// square root of n with Newton-Raphson approximation
r = n / 2;
while ( abs( r - (n/r) ) > t ) {
r = 0.5 * ( r + (n/r) );
}
@kevinpang
kevinpang / gist:1917941
Created February 26, 2012 17:50
StructureMapControllerFactory
public class StructureMapControllerFactory : DefaultControllerFactory
{
public override IController CreateController(RequestContext requestContext, string controllerName)
{
try
{
var controllerType = base.GetControllerType(requestContext, controllerName);
return ObjectFactory.GetInstance(controllerType) as IController;
}
catch (Exception)
@kevinpang
kevinpang / gist:1917944
Created February 26, 2012 17:50
ApplicationStart
protected void Application_Start()
{
ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());
}