Skip to content

Instantly share code, notes, and snippets.

@stephanschubert
stephanschubert / profile.rb
Created September 24, 2013 08:34
Easy profiling w/ ruby-prof
def profile(prefix = "profile")
result = RubyProf.profile { yield }
dir = File.join(Rails.root, "tmp", "performance", params[:controller].parameterize
FileUtils.mkdir_p(dir)
file = File.join(dir, "callgrind.%s.%s.%s" % [prefix.parameterize, params[:action].parameterize, Time.now.to_s.parameterize] )
open(file, "w") { |f| RubyProf::CallTreePrinter.new(result).print(f, :min_percent => 1) }
end
@stephanschubert
stephanschubert / install_mysql2_gem_on_osx.sh
Last active May 25, 2017 08:09
How to install mysql2 gem on OSX
# Assumes you're using homebrew.
# Adjust the version to your convenience.
env ARCHFLAGS="-Os -g -fno-strict-aliasing -arch x86_64" gem install mysql2 -v '0.3.19' -- \
--with-mysql-include=`brew --prefix mysql`/include \
--with-mysql-config=`brew --prefix mysql`/bin/mysql_config
@stephanschubert
stephanschubert / active_data.js.coffee
Last active January 29, 2016 09:17
Simple two-way data binding
do ($ = jQuery, exports = window) ->
class ActiveDataBinder
constructor: (uid) ->
# Use a jQuery object as simple PubSub
pubSub = $ {}
# We expect a 'data' attribute specifying the binding
@stephanschubert
stephanschubert / jquery.reduce.js
Last active December 21, 2015 12:29
The missing counterpart of jQuery.map()
/* jQuery core team does not want to add 'reduce' -
* Details: http://dev.jquery.com/ticket/1886
*/
(function($) {
$.reduce = function(array, callback, initialValue) {
if (Array.prototype.reduce) {
return Array.prototype.reduce.call(array, callback, initialValue);
}
@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?
@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 / 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 / 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 / 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 / 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);
};