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();
}

 
 
Blogger Templates