This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module AuthenticityToken | |
def authenticity_token(&block) | |
class_inheritable_accessor :authenticity_token_generator | |
self.authenticity_token_generator = block | |
include InstanceMethods | |
end | |
module InstanceMethods |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module CurrentMethodName | |
def current_method_name | |
caller[0][/`([^']*)'/, 1] | |
end | |
end | |
# Example: | |
# | |
# class MyClass | |
# include CurrentMethodName |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module CurrentController | |
def self.included(base) | |
base.before_filter :set_current_controller | |
end | |
def set_current_controller | |
Thread.current[:current_controller] = self | |
end | |
end |