Featured Posts


Tuesday, January 26, 2016

Ruby Tail-recursion

Tail-recursion in ruby could be realized with some compile options for ruby VM:
 
RubyVM::InstructionSequence.compile_option = {
  :tailcall_optimization => true,
  :trace_instruction => false
}


More about Tail-recursion.

Tuesday, January 12, 2016

TOR: specify exit node

For specifying exit nodes for Tor connection you could add next two lines to your `/etc/tor/torrc`:

StrictExitNodes 1
ExitNodes {de},{ca},{us} 

Monday, April 6, 2015

Git ignore local files


vim .git/info/exclude - add files like to .gitignore

git update-index --assume-unchanged <file> <file> ... - apply changes

Thursday, February 5, 2015

SQL queries

DEFINITION:
  1. User table
  2. Contract table
  3. User can has many Contract 
  4. Contract has rating field
TASK: Sort user by contract rating, as user can has many contracts we will use latest one

QUERY:
 User.joins('LEFT OUTER JOIN (
             SELECT MAX(id) AS id, user_id FROM contracts GROUP BY user_id
           ) contracts ON contracts.user_id = user.id')
     .joins('LEFT OUTER JOIN contracts contract_fields ON contract_fields.id = contracts.id')
     .order('contract_fields.rating DESC NULLS LAST')

EXPLENATION: first join select proper contract for user and second loads propper fields for sort

to be continued..

Tuesday, January 27, 2015

Export/Import PostgreSQL

EXPORT DB:
pg_dump dbname | gzip > filename.gz
IMPORT DB:
gunzip -c filename.gz | psql dbname


Tuesday, October 28, 2014

Figlet

Figlet is program which translate input data to something like:
._____ ___ ____ _     _____ _____
|  ___|_ _/ ___| |   | ____|_   _|
| |_   | | |  _| |   |  _|   | |
|  _|  | | |_| | |___| |___  | |
|_|   |___\____|_____|_____| |_|

$ echo "Some Test" | figlet
.____                         _____         _
/ ___|  ___  _ __ ___   ___  |_   _|__  ___| |_
\___ \ / _ \| '_ ` _ \ / _ \   | |/ _ \/ __| __|
 ___) | (_) | | | | | |  __/   | |  __/\__ \ |_
|____/ \___/|_| |_| |_|\___|   |_|\___||___/\__|

$ date +"%H : %M : %S" | figlet
.____  _       ____  ____        _ _  _
|___ \/ |  _  |___ \| ___|   _  / | || |
  __) | | (_)   __) |___ \  (_) | | || |_
 / __/| |  _   / __/ ___) |  _  | |__   _|
|_____|_| (_) |_____|____/  (_) |_|  |_|


Saturday, September 27, 2014

Separate number by commas, format phone

Separate number by comma with using `gsub`:
def separate_by_comma(number)
  number.to_s.reverse.gsub(/(\d{3})/, '\1,').reverse
end
without:
def spearate_by_comma(number)
  number.to_s.chars.reverse.each_slice(3).map(&:join).join(",").reverse
end
 
 
Blogger Templates