Skip to content

Instantly share code, notes, and snippets.

@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
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 / 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);
@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 / minmax.js
Created March 28, 2011 15:54
Simple but elegant solution for creating Array#min/max in JavaScript
Array.max = function(array){
return Math.max.apply(Math, array);
};
Array.min = function(array){
return Math.min.apply(Math, array);
};
@stephanschubert
stephanschubert / spork_mongoid_devise_reload_fix.rb
Created April 10, 2011 18:43
Fix the reloading issue when using Spork with Mongoid and/or Devise
Spork.each_run do
# This code will be run each time you run your specs.
load "#{Rails.root}/config/routes.rb"
Dir["#{Rails.root}/app/**/*.rb"].each { |f| load f }
end
@stephanschubert
stephanschubert / cross-browser-background-rgba.scss
Created April 18, 2011 05:44
SCSS mixin for cross-browser background-color: rgba(...). You'll need the Ruby extension for convenient color conversion rgba->hex (IE uses ARGB)
@mixin background-rgba($r,$g,$b,$a) {
// To mimic this in Internet Explorer, you can use the proprietary filter
// property to create a gradient with the same start and end color, along
// with an alpha transparency value.
@if experimental-support-for-microsoft {
$color: ie_hex($r,$g,$b,$a);
$value: unquote("progid:DXImageTransform.Microsoft.gradient(GradientType=0,startColorstr=##{$color},endColorstr=##{$color})");
background-color: transparent\9;
@stephanschubert
stephanschubert / optimized_google_plusone.js
Created June 7, 2011 17:48
Optimized Google PlusOne Javascript - Asynchronous/Non-Blocking, Minimal + SSL Fix
<!-- Place this tag just before your close body tag and NOT in your <head> -->
<script>
(function(d, t) {
var g = d.createElement(t),
s = d.getElementsByTagName(t)[0];
g.async = true;
g.src = 'https://apis.google.com/js/plusone.js';
s.parentNode.insertBefore(g, s);
})(document, 'script');
</script>
@stephanschubert
stephanschubert / fb-extract-comments.php
Created June 15, 2011 21:09
Extract comments from your page on Facebook
<?php
// displays some comments for a certain url
$url = 'http://developers.facebook.com/docs/reference/fql/comment/';
// fql multiquery to fetch all the data we need to display in one go
$queries = array('q1' => 'select post_fbid, fromid, object_id, text, time from comment where object_id in (select comments_fbid from link_stat where url ="'.$url.'")',
'q2' => 'select post_fbid, fromid, object_id, text, time from comment where object_id in (select post_fbid from #q1)',
'q3' => 'select name, id, url, pic_square from profile where id in (select fromid from #q1) or id in (select fromid from #q2)',
);
@stephanschubert
stephanschubert / ssh-reagent
Created September 19, 2011 11:45
Stick this in your shell configuration (e.g. ~/.bashrc or ~/.zshrc) and run ssh-reagent whenever you have an agent session running and a terminal that can't see it.
ssh-reagent () {
for agent in /tmp/ssh-*/agent.*; do
export SSH_AUTH_SOCK=$agent
if ssh-add -l 2>&1 > /dev/null; then
echo Found working SSH Agent:
ssh-add -l
return
fi
done
echo Cannot find ssh agent - maybe you should reconnect and forward it?