Where is instance variable initialized as nil first time?
Can I redefine it as other value by default for all instances?
For example:
class Class
#some code here or maybe in an Object class
end
class Foo1
attr_accessor :bar
end
class Foo2
attr_accessor :bar
end
p Foo1.new.bar # result is not nil
p Foo2.new.bar # result is not nil
This can be done by modifying the reader:
class Class
def attr_accessor(attr_name)
...
define_method "#{attr_name}" do
if instance_variable_get "@#{attr_name}_history"
instance_variable_get "@#{attr_name}_history"
else
"Not nil"
end
end
...
end
end
But this doesn’t help in understanding the core of Ruby.
Many thanks!
Define a new method in class
Class. Get instance variables through:instance_variablesand set them to anything you like by using:instance_variable_set(:@var,default_value)