четверг, 3 октября 2013 г.

Ruby :: Class Variables and Methods

Class methods do not have access to instance methods or instance variables. However instance methods can access both class methods and class variables.

Recall that an instance variable always has an @ symbol prefixed before its name. Similarly, class variables are prefixed with @@.

One of the places where class variables do find proper use is to store application configuration - things like application name, version, database and other settings. The next exercise is to build one such class ApplicationConfiguration. You have to define two class-level methods: set and get. The set method takes two parameters: property_name and value. The get method takes one parameter: property_name, and should return the value that corresponds to the property.

class instance variables

class Item
  def self.show
    puts "Class method show invoked"
  end 
end


class Item
 
  class << self
    def show
      puts "Class method show invoked"
    end
  end
 
end


Ruby :: Instance Variables and Accessors

class Item
 
  def initialize(description, color)
    @description = description
    @color = color
  end
 
  def description=(new_description)
    @description = new_description
  end
   
  def color=(new_color)
    @color = new_color
  end
   
end



rubymonk.com/learning/books/4-ruby-primer-ascent/chapters/45-more-classes/lessons/110-instance-variables#solution4434

Ruby :: Instance Variables and Accessors :: getter and setter methods

So far we talked about accessing the contents of an instance variable through getter methods. Now, how do you change the value of the instance variables from outside? That is where setter methods come into play. 

Ruby :: Redefining, overriding, and super

class Item
    attr_reader :item_name, :qty
   
    def initialize(item_name, qty)
        @item_name = item_name
        @qty = qty
    end
    def to_s
        "Item (#{@item_name}, #{@qty})"
    end

    def ==(other_item)
      puts "Method == called on #{self} with parameter #{other_item}"
      return self.to_s == other_item.to_s
    end
end


p Item.new("abcd",1)  == Item.new("abcd",1)
p Item.new("abcd",2)  == Item.new("abcd",1)
 


class Fixnum
  def +(other)
    42
  end
end



Most object oriented languages offer a mechanism by which an overridden method can be called by the overriding method. Ruby uses the super keyword to make this happen. Using super will call the same method, but as defined in the superclass and give you the result.

class Animal
  def move
    "I can move"
  end
end

class Bird < Animal
  def move
    super + " by flying"
  end
end
 

Ruby :: Understanding Inheritance

puts Float.instance_methods.count #201

puts (Float.instance_methods - Object.instance_methods).count #87

Ruby :: Classification

As you've probably guessed, Object#is_a? accepts a single parameter - a class. Object#is_a? has an alias, Object#kind_of?.

They're identical in every respect so you're free to pick whichever reads better in a given context.

quick summary

  • Classes act as templates for the creation of objects, specifying their state and behaviour
  • An object created by a class is called an instance of that class
  • A class is itself an object, and an instance of the class Class

Ruby :: Blocks, Procs, and Lambdas

The -> literal form is a shorter version of Kernel#lambda.

Kernel#proc factory method is identical to Proc.new. Note that proc is a method and not a literal form like -> nor a keyword like yield.