Skip to content

Instantly share code, notes, and snippets.

View dip00dip's full-sized avatar

Denys Pankratov dip00dip

  • Consultant
  • Lviv, Ukraine
View GitHub Profile
@dip00dip
dip00dip / caesar_cypher.rb
Created October 19, 2017 18:12
sample use of ruby array .zip method for implementing Caesar cypher
letters = Array('a'..'z')
shift = 3
translation_map = letters.zip(letters.rotate(shift)).to_h
"hello".chars.map { |ch| translation_map[ch] }.join
@dip00dip
dip00dip / trunc.sql
Created October 13, 2017 14:20
delete_mass_records
SET temp_buffers = 1000MB -- or whatever you can spare temporarily
CREATE TEMP TABLE tmp AS
SELECT t.*
FROM tbl t
LEFT JOIN del_list d USING (id)
WHERE d.id IS NULL; -- copy surviving rows into temporary table
TRUNCATE tbl; -- empty table - truncate is very fast for big tables
@dip00dip
dip00dip / xxx_remove_delayed_job.rb
Created October 11, 2017 14:32
remove dj table migration
class DropDelayedJobTable < ActiveRecord::Migration
def up
drop_table :delayed_jobs
end
def down
raise ActiveRecord::IrreversibleMigration
end
end
DELIMITER $$
CREATE FUNCTION levenshtein( s1 VARCHAR(255), s2 VARCHAR(255) )
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE s1_len, s2_len, i, j, c, c_temp, cost INT;
DECLARE s1_char CHAR;
-- max strlen=255
DECLARE cv0, cv1 VARBINARY(256);
SET s1_len = CHAR_LENGTH(s1), s2_len = CHAR_LENGTH(s2), cv1 = 0x00, j = 1, i = 1, c = 0;
@dip00dip
dip00dip / delayed_controller.rb
Created February 22, 2013 16:48
API method for adding a job for background processing
class Api::DelayedController < ApplicationController
# Public: Adds a delayed job into queue.
#
# params[:task] - The class name of the job.
#
def add
job = params[:task].constantize
Sidekiq::Client.enqueue job if Object.const_defined?(Sidekiq::Client)
Delayed::Job.enqueue job.new if Object.const_defined?(Delayed::Job)
render :text => "Background job queued", :status => :created
@dip00dip
dip00dip / crontab
Created May 15, 2012 12:28
kill all leaking rails processes runnning via passenger
# every 5 minutes run this script ↓
*/12 * * * * /path/to/kill_memleaked_rails.sh
# Русский перевод для https://github.com/plataformatec/devise/tree/v1.4.2
# Другие переводы на https://github.com/plataformatec/devise/wiki/I18n
ru:
errors:
messages:
expired: "устарела. Пожалуйста, запросите новую"
not_found: "не найдена"
already_confirmed: "уже подтверждена. Пожалуйста, попробуйте войти в систему"
not_locked: "не заблокирована"