Skip to content

Instantly share code, notes, and snippets.

View zedalaye's full-sized avatar

Pierre Yager zedalaye

View GitHub Profile
@sepastian
sepastian / multi_cartesian_product.rb
Last active January 11, 2023 09:26
Build the cartesian product of multiple arrays in Ruby.
# Given an array of arrays s = [ [1,2,3], [4,5,6], ... ]
# compute the Cartesian product among the elements of s.
require 'pp'
s = [[1, 2], [3, 4, 5], [6, 7, 8, 9]]
pp s[1..-1].inject(s[0]){ |m,v| m = m.product(v).map(&:flatten) }
[[1, 3, 6],
[1, 3, 7],
[1, 3, 8],
@karlseguin
karlseguin / gist:2992426
Created June 26, 2012 01:05
sample code used by ranking post
# this somewhat similar code to what I used in http://openmymind.net/Paging-And-Ranking-With-Large-Offsets-MongoDB-vs-Redis-vs-Postgresql/
lids = ['4fe907f1210b2e9e3080f001', '4fe907f1210b2e9e3080f002', '4fe907f1210b2e9e3080f003', '4fe907f1210b2e9e3080f004', '4fe907f1210b2e9e3080f005']
# insert the data
open('data.csv', 'w') do |f|
6000000.times do |i|
f.puts "#{lids.sample},user_#{i},#{(rand() * 10000000).to_i}"
end
end
@rinaldifonseca
rinaldifonseca / default.vcl.pl
Created August 29, 2011 20:31 — forked from bmarini/default.vcl.pl
A good varnish config for a Rails app
# https://www.varnish-cache.org/docs/2.1/tutorial/vcl.html
# https://www.varnish-cache.org/trac/wiki/VCLExamples
# Summary
# 1. Varnish will poll the backend at /health_check to make sure it is
# healthy. If the backend goes down, varnish will server stale content
# from the cache for up to 1 hour.
# 2. Varnish will pass X-Forwarded-For headers through to the backend
# 3. Varnish will remove cookies from urls that match static content file
# extensions (jpg, gif, ...)
@bowsersenior
bowsersenior / sleep_sort.rb
Created June 16, 2011 00:33
Sleep sort in ruby
#!/usr/bin/env ruby
# Outputs sorted args to stdout
# from:
# http://dis.4chan.org/read/prog/1295544154/170
def sleep_sort(*args)
args.each { |e| fork { sleep(e.to_f/1000); puts e } }
end
sleep_sort(*ARGV)