> class ApplicationConfiguration
> @configuration = {}
> def self.set(property, value)
> @configuration[property] = value
> end
> def self.get(property)
> @configuration[property]
> end
> end
> class ERPApplicationConfiguration < ApplicationConfiguration
> @configuration = {}
> end
> class WebApplicationConfiguration < ApplicationConfiguration
> @configuration = {}
> end
> ERPApplicationConfiguration.set("name", "ERP Application")
> WebApplicationConfiguration.set("name", "Web Application")
> p ERPApplicationConfiguration.get("name")
> p WebApplicationConfiguration.get("name")
> p ApplicationConfiguration.get("name")
Class instance variables are a better alternative than class variables simply because the data is not shared across the inheritance chain. Another major difference between class variables and class instance variables are that class instance variables are available only in class methods. But class variables are available in both class methods and instance methods.
A quick summary before we move on:
- Instance variables are available only for instances of a class. They look like @name. Class variables are available to both class methods and instance methods. They look like @@name
- It is almost always a bad idea to use a class variable to store state. There are only a very few valid use cases where class variables are the right choice.
- Prefer class instance variables over class variables when you do really need store data at a class level. Class instance variables use the same notation as that of an instance variable. But unlike instance variables, you declare them inside the class definition directly.