Skip to content

Instantly share code, notes, and snippets.

Alex Kovshovik alex-kovshovik

Block or report user

Report or block alex-kovshovik

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@alex-kovshovik
alex-kovshovik / primes.ex
Created Feb 22, 2018
Print out first N prime numbers using Elixir language
View primes.ex
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 Jan 23, 2017
Destroy many Heroku apps
View herokiri.sh
#!/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 Oct 18, 2016
Run parallel processes in bash
View mrd.sh
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
View exceptions.rb
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 Jun 17, 2016
Ruby exception handling 1
View ruby.rb
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
View migration.rb
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 Apr 19, 2013
This works to concatenate PDFs
View gist:5422059
# 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
#
You can’t perform that action at this time.