Skip to content

Instantly share code, notes, and snippets.

View davidcelis's full-sized avatar
Verified account

David Celis davidcelis

Verified account
View GitHub Profile
@davidcelis
davidcelis / zelda-battery.sh
Created December 18, 2012 00:28
Output a Zelda-style heart meter for your MacBook's battery level. On the command line. Based on an idea from @stephencelis, executed by myself. For a tmux version, see https://gist.github.com/4324139
#!/usr/bin/env zsh
#
# Works best with blinking text; the last heart will blink
# when you have less than 25% of your battery life remaining.
BATTERY="$(pmset -g ps | awk 'NR==2' | perl -pe 's/.*?(\d+)%.*/\1/')"
if [[ $BATTERY -lt 25 ]]; then
echo "\e[5;31m♥\e[0;31m♡♡\e[0m"
elif [[ $BATTERY -lt 50 ]]; then
@davidcelis
davidcelis / githelpers.sh
Created November 21, 2012 20:58
Prune local/remote refs and delete remote branches that have been merged to master
prune_merged() {
# TODO: Test for origin/master vs. master instead.
test -n "$1" && prune_merged_remotes $@ || prune_merged_local
}
prune_merged_local() {
git branch --list --merged |
grep --invert-match --extended-regexp '\*|master' |
xargs -n 1 git branch --delete
}
@davidcelis
davidcelis / falcon.patch
Created November 12, 2012 18:03 — forked from funny-falcon/falcon.patch
Performance patch for ruby-1.9.3-head (applies to p194 as well)
diff --git a/array.c b/array.c
index 64647c3..618d9e3 100644
--- a/array.c
+++ b/array.c
@@ -255,15 +255,24 @@ rb_ary_modify(VALUE ary)
rb_ary_modify_check(ary);
if (ARY_SHARED_P(ary)) {
long len = RARRAY_LEN(ary);
+ VALUE shared = ARY_SHARED(ary);
if (len <= RARRAY_EMBED_LEN_MAX) {
@davidcelis
davidcelis / exceptions.rb
Created September 16, 2012 15:35
Ruby exception scopes in methods
# These two chunks of code are the same, because method have inherent exception scopes
def method
begin
"hello".this_method_doesnt_exist
rescue NoMethodError => e
puts e
end
end
@davidcelis
davidcelis / sorting_benchmark.rb
Created August 13, 2012 17:58
Benchmark between Array#sort and Array#sort_by.
require 'benchmark'
@ids = (1..1000).to_a.shuffle
@records = []
1000.times do |x|
@records << { :id => (x + 1) }
end
Benchmark.bm do |x|
if [[ -x /Applications/MacVim.app/Contents/MacOS/Vim ]]
then
EDITOR="/Applications/MacVim.app/Contents/MacOS/Vim"
elif [[ -x $HOME/Applications/MacVim.app/Contents/MacOS/Vim ]]
then
EDITOR="$HOME/Applications/MacVim.app/Contents/MacOS/Vim"
else
EDITOR="vim"
fi
export EDITOR
@davidcelis
davidcelis / gist:3211214
Created July 30, 2012 22:37 — forked from jimbojsb/gist:1630790
Code highlighting for Keynote presentations

Step 0:

Get Homebrew installed on your mac if you don't already have it

Step 1:

Install highlight. "brew install highlight". (This brings down Lua and Boost as well)

Step 2:

@davidcelis
davidcelis / self_referential_hash.rb
Created July 20, 2012 21:02
How to refer to other elements in the same hash
hash = {
:before => "Hello",
:after => "world!",
:ref => lambda { hash[:before] << ' ' << hash[:after] }
}
hash[:ref].call # => "Hello world!"
@davidcelis
davidcelis / gist:3147089
Created July 19, 2012 21:50
database.yml loads as ERb lulz
$ cat config/database.yml
<% puts "Hello from database.yml" %>
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
$ rails console
Hello from database.yml
Loading development environment (Rails 3.2.6)
@davidcelis
davidcelis / inflections.rb
Created July 19, 2012 16:22
Better Rails Inflections
ActiveSupport::Inflector.inflections do |inflect|
inflect.clear
inflect.plural(/$/, 's')
inflect.plural(/[sxz]|[cs]h$/i, 'es')
inflect.plural(/[^aeiouy]o/i, 'es')
inflect.plural(/([^aeiouy])y/i, '\1ies')
inflect.singular(/s$/i, '')
inflect.singular(/(ss)$/i, '\1')