Skip to content

Instantly share code, notes, and snippets.

View phiggins's full-sized avatar

pete higgins phiggins

View GitHub Profile
require "minitest/autorun"
require "benchmark/ips"
class CachingPack
def self.call(inventory, order)
return if inventory.nil? || inventory.empty?
return if inventory.inject(:+) < order
loop do
r = cached_pack(inventory, order)
for i in $(find app/views/ -type f) ; do echo $(grep -R $(echo $i | sed "s/.*\/_\?\([^.]*\).*/\1/") app/ | wc -l) $i ; done | sort -rn | grep "^0" | sed 's/^0 //' | xargs git rm

Scrabble Cheater

  • I'd like you to help me cheat at Scrabble. I'd like you to write some code to give me all possible words that can be created given a set of tiles. The maximum number of tiles you can have in Scrabble is seven, so you'll need to find all of the words that can be made out of those tiles.

On most unix systems (including Mac OS X), you can find a word dictionary in "/usr/share/dict/words" - let's assume that all of these words (including "zymogenic"!) are up valid for scrabble. Feel free to use your own custom dictionary, but this will do for this purpose.

Here's sample code that reads in all of the words into an array, removes newlines, and downcases them.

words = File.readlines("/usr/share/dict/words").map(&:chomp).map(&:downcase)
branches = %w[ master switch-to-sassc ]
tmp = Pathname.new(Dir.tmpdir)
branches.each do |branch|
asset_dir = tmp + "assets_#{branch}"
if asset_dir.exist?
puts "#{asset_dir} exists, skipping"
next
end
# Find all strings in a CSS file or files that start with a '.' and
# stop on some common CSS delimiters.
# This is only looking at _map.scss, but you could look at any other file or even
# app/assets/stylesheets if you wanted to see everything.
grep -hro "\.[^ .,():]\+" app/assets/stylesheets/partials/mobile/_map.scss |
# The sort | uniq part makes it so we don't look for duplicates.
sort |
uniq |
# Loop over the things we just found.
while read i ; do
# Find all thing strings in a CSS file or files that start with a '.' and
# stop on some common CSS delimiters.
# This is only looking at _map.scss, but you could look at any other file or even
# app/assets/stylesheets if you wanted to see everything.
grep -hro "\.[^ .,():]\+" app/assets/stylesheets/partials/mobile/_map.scss |
# The sort | uniq part makes it so we don't look for duplicates.
sort |
uniq |
# Loop over the things we just found.
while read i ; do
@phiggins
phiggins / gist:7850ccf50d2924f88ad0
Last active December 15, 2016 21:44
bash scripts for finding dead rails code
for i in app/models/post.rb ; do sed -e "s/\s\+def \(self\.\)\?\([^( ]*\).*\|\s\+scope :\([^,]*\).*/\2\3/" -e 'tx' -e 'd' -e ':x' $i | while read j ; do echo $(grep -R $j app/ lib/ config/ | wc -l) $j ; done ; done | sort -rn
for i in $(find app/views/ -type f) ; do echo $(grep -R $(echo $i | sed "s/.*\/_\?\([^.]*\).*/\1/") app/ | wc -l) $i ; done | sort -rn
$ irb
> require 'set'
=> true
> Set.instance_method(:divide)
=> #<UnboundMethod: Set#divide>
> Set.instance_method(:divide).source_location
=> ["/home/pete/.rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/set.rb", 466]
@phiggins
phiggins / benchmark_append.rb
Created October 22, 2014 03:20
What is the fastest way to add one thing to an array without mutating the array?
require 'benchmark/ips'
a = (0..10).to_a
Benchmark.ips do |x|
x.report("push") { a.dup.push(100) }
x.report("shovel") { a.dup << 100 }
x.report("concat") { a.dup.concat([100]) }
x.report("+") { a + [100] }
x.report("unshift") { a.dup.unshift(100) }