Skip to content

Instantly share code, notes, and snippets.

View rafbm's full-sized avatar

Rafael Masson rafbm

View GitHub Profile
@rafbm
rafbm / copy-ip.sh
Created April 15, 2012 02:17
Copy your local IP address from the command line (OS X)
# I often need this IP for testing local websites in VirtualBox
echo -n `ifconfig | grep -Po "(?<=inet )\d*\.\d*\.\d*\.\d*(?=.*broadcast)"` | pbcopy
# Less Unix-y, somewhat more readable version
ifconfig | ruby -e "print STDIN.read.match(/inet (\d*\.\d*\.\d*\.\d*).*broadcast/)[1]" | pbcopy
# I use it like this in my .zshrc
alias ip='echo -n `ifconfig | grep -Po "(?<=inet )\d*\.\d*\.\d*\.\d*(?=.*broadcast)"` | pbcopy'
@rafbm
rafbm / .gitconfig
Created February 6, 2012 16:16
Sane aliases for `git reset`
[alias]
unstage = reset HEAD # unstage
clear = reset --hard HEAD # erase uncommited file edits (destructive!)
uncommit = reset HEAD~1 # erase last commit but keeps related file edits
rollback = reset --hard HEAD~1 # erase last commit and related file edits (destructive!)
@rafbm
rafbm / config.ru
Created January 28, 2012 23:29
Example Sinatra app using the Namespace extension from Sinatra::Contrib
require 'sinatra/base'
require 'sinatra/namespace'
class MyApp < Sinatra::Base
register Sinatra::Namespace
get '/' do
'Home'
end
@rafbm
rafbm / git_aliases.sh
Created December 9, 2011 13:09
Git Aliases for the Minimalist
alias glog="git log --oneline --decorate"
alias gstatus="git status -sbu"
alias gdiff="git diff"
alias gadd="git add -p"
alias gcommit="git commit -v"
alias grebase="git rebase -i"
alias gpull="git pull --rebase origin"
alias gpush="git push origin"
alias gstash="git stash save"
alias gpop="git stash pop"
@rafbm
rafbm / gist:1334218
Created November 2, 2011 17:02 — forked from angus-c/gist:1334100
Arguments default value (allow empty args)
function func(a, f) {
return function(args) {
args = args || {};
args.__proto__ = a;
f.call(this, args);
};
};
var f = func({ foo: 10, bar: 20 }, function(args) {
@rafbm
rafbm / gsub_array.rb
Created September 16, 2011 16:42
String#gsub version that takes two arrays: one as searches and one as replacements
class String
def gsub_array! find, replace
find.each_index do |i|
self.gsub! find[i], replace[i]
end
self
end
def gsub_array find, replace
str = self.clone
@rafbm
rafbm / css-compressor.js
Created August 27, 2011 19:26
CSS compressor
var compressCSS = function(css) {
return css.trim()
.replace(/\s*([{}:;,>+~])\s*/g, '$1') // whitespace
.replace(/;}/g, '}') // trailing semicolons
.replace(/#(\w)\1(\w)\2(\w)\3\b/g, '#$1$2$3') // hex colors
.replace(/(\[[^=]+=)("|')([^"'\s]+)(\2)(\])/g, '$1$3$5') // useless attribute selector quotes
.replace(/\(('|")([^"'\s]+)(\1)\)/g, '($2)') // useless function quotes
}
@rafbm
rafbm / wtf.js
Created August 20, 2011 20:49 — forked from jtaby/wtf.js
this.$().animate({
scale: 1,
translateX: 0,
translateY: 0,
top: document.body.scrollTop,
left: 0,
width: window.innerWidth,
height: window.innerHeight
@rafbm
rafbm / clean-markup-line-numbers.html
Created March 27, 2011 22:23
<table>-free way to display <pre> line numbers that don’t mess up the clipboard.
<html>
<head>
<meta charset="utf-8">
<title>Clean-Markup Line Numbers™</title>
<style>
body {
width: 800px;
margin: 0 auto;
}
@rafbm
rafbm / jquery.in-groups-of.js
Created December 15, 2010 14:21
Returns an array of jQuery objects, grouped by specified number of elements.
/*
* Returns an array of jQuery objects, grouped by specified number of elements.
*
* Say you have 17 <frameset> tags on your page...
*
* $('frameset').inGroupsOf(7); // => [ jQuery[0..6], jQuery[7..13], jQuery[14..16] ]
*
*/
$.fn.inGroupsOf = function( countPerGroup ) {