Do you know problems around class variables in Ruby?
You can declare class variables by using
@@ for prefix of variable name, for instance:
But, class variables can easily overwrite by subclasses. This is based on Ruby specification; class variables can be shared on its subclass.
Class variables are similar with global variables. They're too hard to handle safely.
For usually cases, I can't recommend to use.
Declare Instance Variable on Class object
So then, how we define "class variable," safely?
In Ruby, classes are object. This means you can define instance variable on class.
Scope of instance variables on class are closed within the same class. Thus, they don't effect on subclasses.
(Of course you can use
attr_writer. Example code)
Using from instance
Here are how to use that variables from instance objects.
The simple solution.
but if you wanted to protect from foreigns, you can use
instance_variable_get and private method.