Skip to content

Instantly share code, notes, and snippets.

View thebyrd's full-sized avatar
🌴

David Byrd thebyrd

🌴
View GitHub Profile
@thebyrd
thebyrd / handlebars_template_handler.rb
Created February 21, 2013 19:43
Allows you to render `.hbs` templates that are in the views directory
require 'handlebars'
class HandlebarsHandler < ActionView::Template
def self.call(template)
# return "Hello World!"
<<-RUBY_CODE
template = Handlebars::Context.new.compile('#{template.source}')
vars = {}
partial_renderer = @view_renderer.send(:_partial_renderer)
vars.merge!(@_assigns)
@thebyrd
thebyrd / closures.c
Created February 27, 2013 00:43
Closures in C have a nasty syntax. you can short circuit typedef to create a fake type (in this case iTOi) that can be used as a parameter type.
typedef int (*iTOi) (int a);
int incr (int n) { return n+1; }
int decr (int n) { return n-1; }
void start(iTOi cc) {
printf("%d\n", cc(0));
}
@thebyrd
thebyrd / index.html
Created March 10, 2013 07:13
Playing around with -webkit-transition: 300ms all;
<!DOCTYPE html>
<html>
<head>
<style>
.button a {
-webkit-transition: 300ms all;
width: 32px;
height: 34px;
color: #333;
text-decoration: none;
@thebyrd
thebyrd / app.c
Created March 14, 2013 07:39
How a Javascript Developer reads a file in C.
#include <stdio.h>
int main () {
FILE *file; file = fopen ( "app.c", "rt" );
int c; if (file) while ( ( c = getc(file) ) != EOF ) putchar(c);
file ? fclose(file) : printf("Error: Problem Reading File.\n");
}
@thebyrd
thebyrd / swapDom.js
Last active December 15, 2015 11:19
Just testing how fast it would take to take DOM out of LocalStorage and render it on a page.
var dom="";
dom += " <div id=\"wrapper\">";
dom += " ";
dom += "";
dom += " <div id=\"header\" class=\"header header-logged-in\">";
dom += " <div class=\"container clearfix\">";
dom += " <a class=\"header-logo-blacktocat\" href=\"https:\/\/gist.github.com\/\">";
dom += " <span class=\"mega-icon mega-icon-blacktocat\"><\/span>";
dom += " <span class=\"logo-subbrand\"><img alt=\"GitHub:Gist\" height=\"25\" src=\"https:\/\/gist.github.com\/assets\/modules\/header\/logo-logged-in@4x-6a0620a1bd429fde8a2dd01fb4baef26.png\"><\/span>";
dom += " <\/a>";
@thebyrd
thebyrd / nastyloop.js
Created May 15, 2013 02:33
who knew you could write for loops this way in javascript?
for(var i = 0; i < 10; i++) !function (n) {
console.log(n)
}(i)
@thebyrd
thebyrd / magicMethod.js
Last active December 19, 2015 05:49
Adds jQuery style getters and setters to a given constructor function.
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
}
var getParamNames = function (func) {
var funStr = func.toString()
return funStr.slice(funStr.indexOf('(')+1, funStr.indexOf(')')).match(/([^\s,]+)/g)
}
// Make it Nasty
function increment (i) {
i ^= (i & ~-~i) | (~i & -~i)
return i
}
var list = [1, 3, 5, 5, 23, 23, 4, 3, 3, 6, 7, 4, 2, 2, 5]
var groups = {}
list.forEach(function (n) {
if (groups[n]) groups[n].push(n)
else groups[n] = [n]
})
var result = []
for (var group in groups) result.push(groups[group])
@thebyrd
thebyrd / fatify.js
Created August 21, 2013 03:34
Make all of your tweets look like @fat's
function fatify (str) {
return str.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g,"").toLowerCase()
}