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

Tuesday, August 26, 2014

has_many :grandchildren trick

class Person < ActiveRecord::Base
  belongs_to :parent, class: Person
  has_many :children, class: Person, foreign_key: :parent_id
  has_many :grandchildren, class: Person, through: :children, source: :children
end

Monday, August 25, 2014

Little bit ruby


% Notation
%q[ ] # Non-interpolated String (except for \\ \[ and \])
%Q[ ] # Interpolated String (default)
%r[ ] # Interpolated Regexp (flags can appear after the closing delimiter)
%i[ ] # Non-interpolated Array of symbols, separated by whitespace
%I[ ] # Interpolated Array of symbols, separated by whitespace
%w[ ] # Non-interpolated Array of words, separated by whitespace
%W[ ] # Interpolated Array of words, separated by whitespace
%x[ ] # Interpolated shell command

Sunday, August 24, 2014

Little bit about Graphs

simple - no multiarcs and loop
non-simpl(pseudog) - multiarcs and loop

complete - each node conect beetwen each other directly
conected - directly or nondirectli all nodes are conected
tree - conected graph without circle
spanning tree - tree with (nodes-1 == arcs), min arcs for connect all nodes
isomorphick - graph when (arcs == nodes)
sub - part of graph

ActiveSupport::Inflector

ActiveSupport::Inflector - transforms words from singular to plural, class names to table names, modularized class names to ones without, and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept in inflections.rb. 

Friday, August 22, 2014

PostgreSQL: array field

If we have array field in postgresql (like `tags`), we can use it for search as well:

- search records which contain `tag`
`ruby` = ANY(tags)
- search through array with tags
`{ruby, python, java}` && tags
- all tags should be equal `ruby`
`ruby` = ALL(tags)

Tuesday, October 22, 2013

Shell comand

`cd -` -  toggle between current and last folder
`sudo !!` - run previous command with `sudo`
` <comand>` - without saving to history (space before comand)
`>file.txt` - create new or clean old file
`^foo^bar` - replace in previous command `foo` to `bar`

Saturday, September 7, 2013

Interest browser link (like home page)


Just copy and paste to your browser.

data:text/html,%20<style>html,body{margin:%200;%20padding:%200;}</style><textarea%20style="font-size:%201.5em;%20line-height:%201.5em;%20background:%20%23000;%20color:%20%233a3;%20width:%20100%;%20height:%20100%;%20border:%20none;%20outline:%20none;%20margin:%200;%20padding:%2090px;"%20autofocus%20placeholder="wake%20up%20Neo..."%20/>

Few GIT alias

[alias]
lg = "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
report = "log --author=Marco --since='2 sunday ago' --until='1 sunday ago' --format='%Cgreen%ci%Creset %s%Creset' --no-merges"

report-csv = "log --author=Marco --since='2 sunday ago' --until='1 sunday ago' --format='\"%ci\",\"%s\"' --no-merges"

today = "log --since=midnight --author='Your Initial' --oneline"

Like sort by multiple items

array.sort_by do |item|
  [item.a, item.b]
end