Skip to content

Instantly share code, notes, and snippets.

@stephanschubert
stephanschubert / cache_helper.rb
Created May 2, 2010 13:17
Rails Helper Module: Encodes the asset's timestamp into the request URL as directory which will be removed by Apache's mod_rewrite
module CacheHelper
#
# Implements Google's "Leverage Proxy Caching" tip:
# Instead of using a query parameter as cache buster -
# encode it into the URL to ensure public proxies can
# cache it too.
#
# NOTE: Activates in production mode only because it's
# not necessary for development and needs a mod_rewrite
# rule for Apache anyway:
@stephanschubert
stephanschubert / deferred_google_analytics.html
Created April 9, 2010 10:57
jQuery version of deferred Google Analytics. See http://neil.fraser.name/news/2010/04/07/ for more details.
<script type="text/javascript">
(function($) {
$("body").load(function() {
setTimeout(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
}, 1);
});
})(jQuery);
function t(f) { var x = (new Date()).getTime(); f.call(); return (new Date()).getTime() - x; }
t(function() { for(i = 0; i < 1000000; ++i) { Math.ceil(i); } });
// 1081
t(function() { for(i = 0; i < 1000000; ++i) { ~~i; } });
// 772
@stephanschubert
stephanschubert / euler25.rb
Created February 24, 2010 10:54
Solution for Euler #25 in Ruby w/ memoization
# http://projecteuler.net/index.php?section=problems&id=25
# There are memoization libs, but i had it at my
# fingertips anyway ..
def memoize(name)
cache = {}
(class << self; self; end).send(:define_method, name) do |*args|
cache[args] = super(*args) unless cache.has_key?(args)
cache[args]
end
@stephanschubert
stephanschubert / gist:296073
Created February 5, 2010 18:39
Recursive fibonacci with memoization
def memoize(name)
cache = {}
(class << self; self; end).send(:define_method, name) do |*args|
cache[args] = super(*args) unless cache.has_key?(args)
cache[args]
end
end
def fib(n)
return n if n < 2
@stephanschubert
stephanschubert / apache_asset_config
Created December 2, 2009 18:16
Apply a cachebuster to your static assets before using this Apache configuration
<FilesMatch "\.(pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "public"
ExpiresActive On
ExpiresDefault "access plus 10 years"
FileETag None
Header unset Last-Modified
Header unset ETag
</FilesMatch>
@stephanschubert
stephanschubert / authenticity_token.rb
Created December 1, 2009 11:58
Ruby module for convenient authenticity token generation and usage for any class
module AuthenticityToken
def authenticity_token(&block)
class_inheritable_accessor :authenticity_token_generator
self.authenticity_token_generator = block
include InstanceMethods
end
module InstanceMethods
@stephanschubert
stephanschubert / google_analytics_real_bounce_rate_fix.rb
Created November 21, 2009 12:05
Track your real bounce rate by triggering a custom no-bounce event with Google Analytics after a given timeout.
def google_analytics_real_bounce_rate_fix(timeout = 10)
name = "NoBounce"
msg = "Over #{timeout} seconds"
ms = timeout * 1000
<<-JS
function disqualifyAsBounce() {
pageTracker._trackEvent('#{name}', '#{name}', '#{msg}');
}
setTimeout("disqualifyAsBounce()", #{ms});
@stephanschubert
stephanschubert / current_method_name.rb
Created November 18, 2009 12:11
Ruby module providing convenient access to the currently called method's name
module CurrentMethodName
def current_method_name
caller[0][/`([^']*)'/, 1]
end
end
# Example:
#
# class MyClass
# include CurrentMethodName
@stephanschubert
stephanschubert / current_controller.rb
Created November 6, 2009 11:34
A small Ruby module which provides access to the current controller everywhere you could need it. Works in multithreaded environments.
module CurrentController
def self.included(base)
base.before_filter :set_current_controller
end
def set_current_controller
Thread.current[:current_controller] = self
end
end