Skip to content

Instantly share code, notes, and snippets.

View dhotson's full-sized avatar

Dennis Hotson dhotson

View GitHub Profile
<?php
class DomainObject
{
public function __construct($arr = array())
{
foreach($arr as $key => $value)
$this->$key = $value;
$this->_attributes = new Attributes();
<?php
$english = arr(
strr('Hello X'),
strr('Goodbye X')
);
$pirate = $english
->replace('/Hello/', 'Avast')
->replace('/Goodbye/', 'Was good te see ye')
jQuery.fn.noisy = function(opacity) {
if (typeof(opacity) === 'undefined') {
opacity = 0.1;
}
var wrapper = jQuery(this).wrapInner('<div />').children();
var canvas = document.createElement("canvas");
canvas.width = 100;
canvas.height = 100;
var ctx = canvas.getContext("2d");
<?php
require_once 'oo.php'
// A subclass of 'class' that lets you define classes in XML ;-)
$xmlclass = $class->extend()
->fn('load', function($t, $xml) {
$class = $t->new();
$doc = new DOMDocument();
$doc->loadXML($xml);
<?php
$array = function() {
$elements = func_get_args();
$result = fn(function($i) use ($elements) {
return $elements[$i];
});
$result->len = function() use ($elements) {
@dhotson
dhotson / oo.php
Created December 6, 2011 12:02
PHP Object Oriented Programming Reinvented (for PHP 5.4)
<?php
// Define the 'class' class
$class = (new Obj)
->fn('new', function() {
$newClass = (new Obj($this->methods))
->fn('new', function() {
$obj = new Obj($this->imethods);
call_user_func_array(array($obj, 'init'), func_get_args());
return $obj;
@dhotson
dhotson / oedipus.rb
Created May 14, 2012 11:08
Oedipus Sphinx2 rails instrumentation
# config/initializers/oedipus.rb
require "oedipus"
# Monkey patch in oedipus instrumentation
module Oedipus
class LogSubscriber < ActiveSupport::LogSubscriber
def search(event)
output = "%s (%.2fms)" % ['Sphinx', event.duration]
debug " #{color(output, RED, true)}"
dep "sphinx.src" do
source "http://sphinxsearch.com/files/sphinx-2.0.4-release.tar.gz"
define_var :base_dir, :default => Pathname.new(File.dirname(__FILE__) + "/../lib/sphinx2").cleanpath
provides var(:base_dir) + "/bin/searchd"
prefix var(:base_dir)
install { shell "make install" }
meet do
shell "mkdir -p #{var(:base_dir) / 'binlog'}"
build tools {
build-essential {
apt {
package manager {
'apt-get' runs from /usr/bin.
} ✓ package manager
apt source {
} ✓ apt source
apt source {
} ✓ apt source
@dhotson
dhotson / chipmunk.rb
Created June 22, 2012 13:23
Example of Chipmunk Physics library usage in Ruby
# See: https://github.com/beoran/chipmunk/tree/master/spec
# + http://chipmunk-physics.net/release/ChipmunkLatest-Docs/
require 'chipmunk' # gem install chipmunk
s = CP::Space.new
s.gravity = vec2(0,0)
b1 = CP::Body.new(5, 7)
b1.pos = vec2(400,600)
b1.v = vec2(30,1)