Skip to content

Instantly share code, notes, and snippets.

View acapilleri's full-sized avatar

Angelo Capilleri acapilleri

View GitHub Profile
@acapilleri
acapilleri / permutations
Created October 7, 2015 16:20
Permutations in Ruby from scratch
def get_perm str
return [str] if str.size == 1
perms = []
first_char = str.slice!(0)
words = get_perm(str)
words.each do |word|
perms += insert_each(first_char, word)
end
perms
def mergesort(ary)
return ary if ary.count <= 1
size = ary.count
m = size / 2
merge mergesort(ary[0..m-1]), mergesort(ary[m..size-1])
end
def merge(a, b)
return a if b.count <= 0
return b if a.count <= 0
@acapilleri
acapilleri / ruby-quicksort.rb
Last active August 29, 2015 14:26
Ruby Quick Sort
def quicksort(ary)
size = ary.count
return ary if size <= 1
i, j = 0, size - 1
pivot = ary[(size / 2)]
while i < j
i += 1 while ary[i] < pivot
j -= 1 while ary[j] > pivot
ary[i], ary[j] = ary[j], ary[i]
@acapilleri
acapilleri / gist:6898533
Last active December 25, 2015 01:49
Rails App ruby 2.1 MRI vs Rubinius 2.0
Server:
iMac
Mac10,1
Intel Core 2 Duo
3,06 GHz
Cache L2: 3 MB
RAM: 8 GB
bus: 1,07 GHz
@acapilleri
acapilleri / gist:6182210
Last active December 20, 2015 19:19
Generate PDF with wkhtmltopdf without any required gems. In this case I use rails, but it's flexible for any ruby frameworks or just for simply ruby app
def show
@pet = Pet.find(params[:id])
tempfile = Tempfile.new(@pet.id.to_s)
render_file = Tempfile.new(@pet.id.to_s + '.html')
buffer = render 'show.pdf.erb', layout: false
render_file.write(buffer.first)
html = "#{render_file.path}.html"
system "mv #{render_file.path} #{html} && wkhtmltopdf #{html} #{tempfile.path}"
send_file tempfile.path, type: 'application/pdf'