Skip to content

Instantly share code, notes, and snippets.

@jish
jish / collision.rb
Last active December 31, 2015 05:09
If I randomly guess a number between 1 & 1000, when will the numbers collide?
def detect_collision(number)
guessed_numbers = {}
count = 0
collision = false
while !collision
guess = rand(number)
count += 1
if guessed_numbers[guess]
@jish
jish / branch_cleanup.rb
Created July 15, 2013 02:02
Branch cleanup. Made this a private gist to begin with, but people keep asking me for a link to it. Horray for open source! (:
#!/usr/bin/env ruby
puts "Starting..."
branches = `git branch --merged`.split("\n").map { |branch| branch.strip }
pattern = (ARGV.shift || `whoami`).strip
branches.select! { |branch| branch.match(pattern) }
count = branches.size
@jish
jish / percent.rb
Created June 16, 2013 22:55
percent
# Q: How do I make something happen 25% of the time
# A: rand(4) == 0
# How?
results = Hash.new(0)
10_000.times do
results[rand(4)] += 1
end
@jish
jish / gifify
Created April 5, 2013 01:48
gifify
#!/usr/bin/env sh
if [[ "$1" && "$2" ]]; then
tmp='tmp.gif'
ffmpeg -i $1 -pix_fmt rgb24 $tmp
convert -layers Optimize $tmp $2
rm $tmp
else
echo "usage: gifify <input.mov> <output.gif>"
fi
@jish
jish / gist:5049205
Created February 27, 2013 16:23
Does the filename matter when taking an md5 or shasum of a file?
~/tmp% echo "Hello World" > file1.txt
~/tmp% echo "Hello World" > file2.txt
~/tmp% shasum file1.txt
648a6a6ffffdaa0badb23b8baf90b6168dd16b3a file1.txt
~/tmp% shasum file2.txt
648a6a6ffffdaa0badb23b8baf90b6168dd16b3a file2.txt
~/tmp% md5 file1.txt
MD5 (file1.txt) = e59ff97941044f85df5297e1c302d260
~/tmp% md5 file2.txt
MD5 (file2.txt) = e59ff97941044f85df5297e1c302d260
@jish
jish / install.sh
Created December 18, 2012 15:57
brew install ruby-build
brew install ruby-build
==> Installing ruby-build dependency: rbenv
==> Downloading https://github.com/sstephenson/rbenv/tarball/v0.3.0
######################################################################## 100.0%
Warning: The cleaning step did not complete successfully
Still, the installation was successful, so we will link it into your prefix
==> Caveats
To enable shims and autocompletion add to your profile:
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
@jish
jish / tax.rb
Created October 13, 2012 00:31
def tax_brackets(salary)
current = salary
total = 0
if salary > 8_700
total += 8_700 * 0.10
else
return salary * 0.10
end
require 'bundler'
Bundler.setup(:assets)
require 'rake-pipeline-web-filters'
RequireFilter = Rake::Pipeline::Web::Filters::NeuterFilter
REQUIRE_REGEX = %r{//= require (.*)}
output 'public'
load_paths = ['app/assets/javascripts', 'vendor/assets/javascripts']
@jish
jish / protected.rb
Created March 20, 2012 04:42
Private vs. Protected in Ruby
class Foo
protected
def protected_method
puts "protected method"
end
private
@jish
jish / .mutt<slash>aliases
Created January 19, 2012 19:35
Mutt aliases
alias foo Foo Bar <[email protected]>