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

Wednesday, September 17, 2014

Rails: STI (Single Table Inheritance)

class User < ActiveRecord::Base
has_many :animals
has_many :dogs
has_many :cats
end
class Zoo < ActiveRecord::Base
has_many :animals
has_many :elephants
end
class Animal < ActiveRecord::Base
# for STI should have additional field `type`
scope :domestic, -> { where("user_id IS NOT NULL").includes(:user) }
end
class Elephant < Animal
belongs_to :zoo
end
class Dog < Animal
belongs_to :user
end
class Cat < Animal
belongs_to :user
end
user = User.first
user.animals # -> [<Dog:...>, <Cat:...> ..]
user.dogs # -> [<Dog:...>, ..]
user.cats # -> [<Cat:...>, ..]
zoo = Zoo.first
zoo.animals # -> [<Elphant:...>, ..]
zoo.elephants # -> [<Elphant:...>, ..]
Animals.all # -> [<Dog:...>, <Cat:...>, <Elephants:..>, ..]
Dogs.all # -> [<Dog:...>, ..]
Cats.all # -> [<Cat:...>, ..]
Elephant.all # -> [<Elephants:...>, ..]
Animals.domestic # -> [<Dog:...>, <Cat:...> ..]
view raw animal.rb hosted with ❤ by GitHub


Tuesday, September 16, 2014

Rust: macros notation

//
// #[item_attribute] - attribute for module or file
//
// #![crate_attribute] - attribute for crate
//
// - #[attribute = "value"]
// - #[attribute(key = "value")]
// - #[attribute(value)]
//
// #![crate_type = "lib"] - crate as library (instead `--crate-type` compile param)
// #![crate_name = "something"] - library is named "something"
//
#[cfg(target_os = "linux")]
fn are_you_on_linux() {
println!("you are running linux!")
}
#[cfg(not(target_os = "linux"))]
fn are_you_on_linux() {
println!("you are *not* running linux!")
}
#[cfg(not(some_key))]
fn conditional_function() {
println!("you don't pass `--cfg some_key`!")
}
#[cfg(some_key)]
fn conditional_function() {
println!("you just pass `--cfg some_key`!")
}
// allow unused code
#[allow(dead_code)]
// allow camel case type names
#[allow(non_camel_case_types)]
// add default Show trait implementation to struct
#[deriving(Show)]
struct SomeStructure (int, int);
fn main() {
are_you_on_linux();
conditional_function();
println!("test deriving Show - {}", SomeStructure(1,2));
}
view raw attributes.rs hosted with ❤ by GitHub


Monday, September 15, 2014

Rust: some notation about Trait methods

// file_1.rs
//
// implement class method and instance method with the same name
//
struct Car {
stamp: &'static str
}
impl Car {
fn print() {
println!("It is Car class method!")
}
}
trait Print {
fn print(&self) -> ();
}
impl Print for Car {
fn print(&self) {
println!("It is Car instance method")
}
}
fn main() {
let car = Car { stamp: "BMW" };
Car::print();
car.print();
}
// file_2.rs
//
// traits couldn't implement class methods
// but could implement class methods which return Self (because with
// type it knows what implementation should use)
//
struct Car {
stamp: &'static str
}
impl Car {
fn print(&self) {
println!("It is Car instance with stamp: {}", self.stamp)
}
}
trait Face {
fn print() -> ();
fn new(var: &'static str) -> Self;
}
impl Face for Car {
fn print() {
println!("It is Car class method!")
}
fn new(var: &'static str) -> Car {
Car { stamp: var }
}
}
fn main() {
// Car::print(); // first-class methods are not supported
// Face::print(); // cannot determine a type for this bounded type parameter: unconstrained type
// let car = Face::new("BMV"); // cannot determine a type for this bounded type parameter: unconstrained type
let car: Car = Face::new("BMV");
car.print();
}


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)
 
 
Blogger Templates