Skip to content

Instantly share code, notes, and snippets.

Avatar

Isak Sky isaksky

View GitHub Profile
@isaksky
isaksky / letter_combos.rb
Created Nov 28, 2011
Ruby: All letter combinations of length n
View letter_combos.rb
LETTERS = (65..90).collect{|char_code| char_code.chr}
def letter_combos length, accum = nil
return accum if length == 0
return accum || LETTERS if length == 1
combos = (accum || LETTERS).product(LETTERS).inject([]) {|combos, product|
combos << product.first + product.last}
letter_combos(length - 1, combos)
end
@isaksky
isaksky / gist:1861043
Created Feb 18, 2012
HTTP fetch breaks
View gist:1861043
require 'uri'
require 'net/http'
# From http://ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTP.html#method-c-get_response
# Does not fully work!
def fetch(uri_str, limit = 10)
# You should choose a better exception.
raise ArgumentError, 'too many HTTP redirects' if limit == 0
response = Net::HTTP.get_response(URI(uri_str))
@isaksky
isaksky / gist:1861104
Created Feb 18, 2012
get_response_following_redirects
View gist:1861104
def self.get_response_following_redirects orig_uri, requests_limit = 15
raise "Input must be an absolute URI." unless orig_uri.is_a?(URI::Generic) && orig_uri.absolute?
uri = orig_uri
requests_made = 0
while requests_made <= requests_limit
response = Net::HTTP.get_response uri
requests_made += 1
case response
when Net::HTTPSuccess
View okcupid.com.js
// How to win. Script for okcupid.com
// Using a script manager, e.g., this thing: https://github.com/defunkt/dotjs
// Replace cities_close_enough array with what is close enough for you.
// Drop this in ~/.js/okcupid.com.js
window.setInterval(function(){
$('p.location:visible').each(function(){
var loc = $(this).text();
var city = loc.substring(0,loc.indexOf(','));
var cities_close_enough = ['Palo Alto', 'Mountain View', 'Sunnyvale', 'Stanford'];
View cartesian_product.rb
def ghetto_cartesian_product colls
if colls.empty?
[[]]
else
colls.first.reduce([]) do |memo, e|
rest = colls.drop 1
tails = ghetto_cartesian_product(rest).map do |tail|
[e].concat tail
end
memo.concat tails
@isaksky
isaksky / install_rbenv_stuff.sh
Created Jul 18, 2012
Script to install rbenv and ruby-build
View install_rbenv_stuff.sh
# Check out rbenv into ~/.rbenv.
cd
git clone git://github.com/sstephenson/rbenv.git .rbenv
# Add ~/.rbenv/bin to your $PATH for access to the rbenv command-line utility.
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
# Add rbenv init to your shell to enable shims and autocompletion.
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
@isaksky
isaksky / gist:3854955
Created Oct 8, 2012
set/access multiple keys
View gist:3854955
update_in = (obj, keys, value) ->
node = obj
while true
key = keys.shift()
if keys.length > 0
node[key] = {} unless node[key]?
node = node[key]
else
node[key] = value
break
View gist:4541357
function video_info {
ffprobe -v quiet -print_format json $1 -show_streams
}
View gist:4666169
task :foo do
puts "foo"
end
task :bar => "foo" do
puts "bar"
end
task :baz do
puts "baz"
View gist:5059818
var process = Process.Start(new ProcessStartInfo("foo.exe"));
process.StandardInput.Write("sup");