Introduction
In Ruby, variables are classified into four types: local variables, instance variables, class variables, and global variables. In Ruby, an instance variable has a name that begins with the @ symbol, and its content is limited to whatever the object itself references to. Even though two objects belong to the same class, they can have distinct values for their instance variables.
Characteristics of Instance Variables
- Before initialization, the instance variable has the value nil.
- By default, all instance variables are private.
- The instance variables of an object can only be accessed through the object's instance methods.
- Ruby instance variables do not need to be declared. This means that the object structure is adaptable.
- When an object is first referenced, every instance variable is dynamically attached to it.
- An instance variable belongs to the object (each object has its own instance variable of that specific class). One instance object can change the values of its instance variables without affecting any other instances.
- Except when the method is regarded as static, an Instance variable can be utilized by numerous class methods.
When you write a constructor for a class that takes several arguments, where each argument is assigned to an instance variable. For example, there is an RGBColor class that has three instance variables, i.e., red, green, and blue. To assign a value to each of the instance variables, we need to write the following code.
class RGBColor(red=0, green=0, blue=0)
@red = red
@blue = blue
@green = green
end
You'd like to avoid all of the typing required to complete those variable assignments.
Automatic Initialization of Instance Variables
To overcome the above-discussed problem there is a method that helps us to automatically initialize the instance variables. It takes as an argument the list of variables supplied to the initialize method, as well as the variables' binding to values. You can avoid the time-consuming variable assignments by using this method:
Code
class Object
def set_instance_variables(binding, *variables)
variables.each do |var|
instance_variable_set(:"@#{var}", eval(var.to_s, binding))
end
end
end
class RGBColor
def initialize(red=0, green=0, blue=0)
set_instance_variables(binding, *local_variables)
end
end
RGBColor.new(255, 0, 255)
Our set instance variables method, accepts a list of parameter names to be converted into instance variables, as well as a Binding containing the values of those arguments at the time of the method call. An eval statement binds the corresponding instance variable to the corresponding value in the Binding for each argument name.
Since the names of a method's parameters are not available from Ruby code, how can we obtain that list? When you call a method, any arguments you give in are immediately bound to local variables. These are the only local variables defined at the start of the method.
When you call a method, any arguments you pass in are immediately bound to local variables. These are the only local variables defined at the start of the method. This means that at the start of a method, invoking Kernel#local variables will return a list of all the parameter names.
If your function accepts parameters that you do not want to set as instance variables, just delete their names from the list returned by Kernel#local variables before passing it to set instance variables:
Code
class RGBColor
def initialize(red=0, green=0, blue=0, debug=false)
set_instance_variables(binding, *local_variables-['debug'])
puts "Color: #{red}/#{green}/#{blue}" if debug
end
end
RGBColor.new(10, 200, 255, true)