четверг, 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


Комментариев нет:

Отправить комментарий