Skip to content

Instantly share code, notes, and snippets.

# 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?
# instead of:
if retail_chains && retail_chains.size > 0
return retail_chains[0]
end
# you could write:
retail_chains.first unless retail_chains.blank?
# 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
many_elements.each do |foo|
dict[foo] ||= expensive_computation
end
var ||= the_value
# rather than
if some_condition
return true
else
return false
end
# you can use
@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
@lightningdb
lightningdb / goodreads-bookmarklet.js
Last active November 22, 2023 17:36
Add to Goodreads from Amazon.com book page bookmarklet
javascript: var asin_elements, asin;asin_elements = document.getElementsByName('ASIN'); if (asin_elements.length == 0) { asin_elements = document.getElementsByName('ASIN.0'); };if (asin_elements.length == 0) { alert('Sorry, this doesn\'t appear to be an Amazon book page.'); }else { asin = asin_elements[0].value; if (asin.match(/\D/) === null) { var x = window.open('http://www.goodreads.com/review/isbn/'+ asin, 'add_review'); } else { var x = window.open('https://www.goodreads.com/search?q='+ asin); } x.focus();}