Skip to content

Instantly share code, notes, and snippets.

#!/bin/sh
# Some things taken from here
# https://github.com/mathiasbynens/dotfiles/blob/master/.osx
# Set the colours you can use
black='\033[0;30m'
white='\033[0;37m'
red='\033[0;31m'
green='\033[0;32m'
# brainfreeze!
# what is the idiomatic ruby for this:
# if the var is an array, do_something with each element, otherwise just do something with the var
if var.is_a?(Array)
var.each do |element|
do_something(element)
end
else
do_something(var)
end
# For Level 16 developers
# Instead of:
if !retailers.empty?
# Use this:
unless retailers.empty?
# longer and harder to read, with redundant lines
facet_values_after = []
facet_values.each do | facet_value |
facet_value = facet_value.gsub(/\_/, '\-')
facet_values_after << facet_value.gsub(/"/, '\"')
end
return facet_values_after
# can be turned into
# instead of:
if retail_chains && retail_chains.size > 0
return retail_chains[0]
end
# you could write:
retail_chains.first unless retail_chains.blank?
var ||= the_value
# rather than
if some_condition
return true
else
return false
end
# you can use
many_elements.each do |foo|
dict[foo] ||= expensive_computation
end
@lightningdb
lightningdb / OptionExample.rb
Created June 23, 2010 23:11
OptionHashExample.rb
# A method signature such as:
def blah(file, underscore_to_hyphen=true)
# will be called like so:
blah(file, true)
# where true could also be false
# this is a code smell, since it is impossible to tell
# from the call what the boolean does
@lightningdb
lightningdb / publish.sh
Created April 6, 2012 23:58
Publish documentation to directory reflecting tag or branch in gh-pages branch
#!/bin/sh
# get current branch and tag
branch=`git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3`;
tag=`git describe --tags`;
# if current tag has annotations showing number of commits since tagging, then use current branch
destination="$tag"
if [[ "$tag" == *-* ]]
then