Skip to content

Instantly share code, notes, and snippets.

@craigp
craigp / gist:504802
Created August 2, 2010 15:20
duration_for
#!/usr/bin/env ruby
def duration_for spec
ret = nil
if((m = %r/^ (\d+(?:\.\d+)?) : (\d+(?:\.\d+)?) : (\d+(?:\.\d+)?) $/iox.match(spec.to_s)))
m, h, m, s, ignored = m.to_a
h, m, s = Float(h), Float(m), Float(s)
ret = (h * 60 * 60) + (m * 60) + (s)
else
pat = %r/(\d+(?:\.\d+)?)\s*([sSmMhHdDwWyY][^\d]*)?/
@craigp
craigp / gist:874431
Created March 17, 2011 14:40
Sinatra caching code I found lying around on my machine and wanted to save somewhere
require 'memcache'
require 'digest/md5'
module Sinatra
class Cache
def self.cache(key, &block)
unless CONFIG['memcached']
raise "Configure CONFIG['memcached'] to be a string like 'localhost:11211' "
end
begin
" Vim color scheme
"
" My version of the Railscast VIM color scheme originally by:
"
" Name: railscast.vim
" Original Maintainer: Josh O'Rourke <[email protected]>
" License: public domain
"
" A GUI Only port of the RailsCasts TextMate theme [1] to Vim.
" Some parts of this theme were borrowed from the well-documented Lucius theme [2].
@craigp
craigp / gist:3761932
Created September 21, 2012 14:52
Hash deep merge
class Hash
def deep_merge(other_hash)
Hash.new.replace(self).tap do |new_hash|
new_hash.deep_merge!(other_hash)
end
end
def deep_merge!(other_hash)
each do |key, val|
@craigp
craigp / sinatra-memcacher.rb
Created November 27, 2012 13:53 — forked from mralex/sinatra-memcacher.rb
Simple memcached helper for Sinatra.
require 'sinatra/base'
require 'memcached'
module Sinatra
module Memcacher
module Helpers
def cache(key, &block)
return block.call unless options.memcacher_enabled
begin
defmodule ExList do
def span(from, to) when from < to do
_span([], from, to)
end
defp _span(list, from, to) when from > to do
list
end
// var threadsToLoad = [],
// _thread;
// restrict number of threads to load
var threadsToLoad = (function _getThreads(loadThreads, allThreads) {
var thread = allThreads.shift();
if (thread && loadThreads.length < settings.maxSourceListItemsToCache) {
loadThreads.push(thread);
return _getThreads(loadThreads, allThreads);
} else return loadThreads;
function apocalypse(exitOnError) {
process.on('uncaughtException', function(err) {
process.removeListener('uncaughtException', arguments.callee);
logger.fatal(err);
if (exitOnError) {
var s = logger.streams[0];
define(['common', 'async'], function(common, async) {
'use strict';
function Emitter() {
this.events = {};
}
(function p(msg, meta, formatArgs, args) {
var arg = args.shift();
if (arg) {
if (typeof(arg) !== 'object')
formatArgs.push(arg);
else
meta = arg;
p(msg, meta, formatArgs, args);
} else
self._logger[level].apply(self._logger, [].concat.apply([], [meta, msg, formatArgs]));