Skip to content

Instantly share code, notes, and snippets.

View roberthead's full-sized avatar

Rob Head roberthead

View GitHub Profile
@roberthead
roberthead / rails_template.rb
Created July 29, 2015 22:27
A Rails template for rails 4.2 with rspec, slim, sass, coffeescript
use_devise = ENV['devise'] || false
create_homepage = ENV['home'] || false
git :init
append_file ".gitignore", "config/database.yml"
run "cp config/database.yml config/example_database.yml"
git add: '.'
git commit: "-m 'Initial commit'"
@roberthead
roberthead / roundTo.coffee
Last active August 29, 2015 14:10
Rounding with precision in CoffeeScript
Math.roundTo = (number, precision) ->
Math.round(number * 10**precision) / 10**precision
@roberthead
roberthead / roundTo.coffee
Last active August 29, 2015 14:10
Math.roundTo in CoffeeScript
Math.roundTo = (number, precision) ->
+(Math.round(number + "e+#{precision}") + "e-#{precision}")
@roberthead
roberthead / enumerable_histogram.rb
Last active December 27, 2015 03:19
Some histogram-related methods for ruby Enumerable types.
module Enumerable
# Return a Hash whose keys are the unique elements and whose values are the number of appearences of that element.
# [2,1,2,3,4,1,2,4,9,1,3,16,8].histogram => {2=>3, 1=>3, 3=>2, 4=>2, 9=>1, 16=>1, 8=>1}
def histogram
inject(Hash.new(0)) { |hash, x| hash[x] += 1; hash }
end
# Return an enumerable with the same elements sorted from the most frequent to least frequent.
# See: http://codeidol.com/other/rubyckbk/Arrays/Sorting-an-Array-by-Frequency-of-Appearance/
# [2,1,2,3,4,1,2,4,9,1,3,16,8].sort_by_frequency => [2, 2, 2, 1, 1, 1, 3, 3, 4, 4, 9, 16, 8]