Skip to content

Instantly share code, notes, and snippets.

View alex-kovshovik's full-sized avatar
🧠
Experimenting with things

Alex Kovshovik alex-kovshovik

🧠
Experimenting with things
View GitHub Profile
@alex-kovshovik
alex-kovshovik / primes.ex
Created February 22, 2018 20:16
Print out first N prime numbers using Elixir language
defmodule Primes do
# Print out first N prime numbers
def get(n) do
Stream.iterate(1, &(&1 + 1))
|> Enum.reduce_while(0, fn number, count ->
print_if_prime(n, count, number)
end)
end
defp print_if_prime(n, count, _) when count >= n, do: {:halt, count}
@alex-kovshovik
alex-kovshovik / herokiri.sh
Created January 23, 2017 17:05
Destroy many Heroku apps
#!/bin/bash
# THIS DESTROYS MANY HEROKU APPS!!!!
# USE CAUTION!
# ИСПОЛЬЗУЙ ВНИМАНИЕ!
KEYWORD = diesel
heroku apps -A | grep $KEYWORD | cut -d' ' -f1 | xargs -L1 bash -c 'app=$0; heroku apps:destroy --app $app --confirm $app'
@alex-kovshovik
alex-kovshovik / mrd.sh
Created October 18, 2016 13:18
Run parallel processes in bash
rm -rf mrd.log
rm -rf mrd.err
# Anything in round braces is a subshell. By default it runs in foreground as well,
# but if you add "&" at the end - it would run as a sub-process.
(
echo "Waiting for mrd to start."
while true; do
sleep 1s
@alex-kovshovik
alex-kovshovik / exceptions.rb
Created June 17, 2016 23:12
Ruby exceptions 2
def export_to_csv(csv, record)
row = []
row << record.id
row << record.title
row << record.owner.name
row << record.get_validation_data(RecordValidator.instance)
# More boring-ass code like that here.
# ...
rescue Exception => ex
error = Exception.new("#{ex.message}, on #{record.class} ID #{record.id}")
@alex-kovshovik
alex-kovshovik / ruby.rb
Created June 17, 2016 23:12
Ruby exception handling 1
def export_to_csv(csv, record)
row = []
row << record.id
row << record.title
row << record.owner.name
row << record.get_validation_data(RecordValidator.instance)
# More boring-ass code like that here.
# ...
end
@alex-kovshovik
alex-kovshovik / migration.rb
Created June 17, 2016 23:08
Rails migration
class MigrateToNewContentStructure < ActiveRecord::Migration
FIND_EMBEDDED_RECIPE_REGEX = /!insert_recipe\((\d+)\)/
def up
ActiveRecord::Base.transaction do # Just wrap the bastard in the transaction block.
Recipe.all.each do |recipe|
recipe.content_block = ContentBlock.create!(...)
recipe.save!
end
@alex-kovshovik
alex-kovshovik / gist:5422059
Created April 19, 2013 18:04
This works to concatenate PDFs
# This class uses the pdftk toolkit.
class PdfToolkit
# Merges some pdf files to output file
#
# options
# :from_files - array of source pdf files
# :to_file - output file
#