Wednesday, October 2, 2013

Ruby first

Before i'll start learning ruby on rails framework, i'm going to learn some basics of ruby language.

Variables:

hello_world = "";
variable = 10;

Methods:

def method_name
  return "Something";
end

def method_name(arg1, arg2)
  puts arg1.to_s + " " + arg2.to_s;
end

Classes:

class ClassName
  // Constructor
  def initialize
    @price = 30
  end

  // Variable @price and getter of price
  def price
    @price
  end

  // Setter of price.
  def price=(value)
    @price = value
  end
end

vari = ClassName.new;
puts vari.price; // Prints 30
vari.price = 10; // Same as vari.price=(10)
puts vari.price; // Prints 10

class ClassName
  attr_reader :price; // Same as getter. attr_reader :price, :weight, :lol;
  attr_writer :price;  // Same as setter
  //attr_accessor :price; // This method creates getters and setters together.
end

Anonymous functions (blocks)

yield :)

No comments:

Post a Comment