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