I have this gem of a parser that does:
def parse_data data
data.chomp!
while data && data.length > 0
data_array = data.split(/\r?\n/,2)
@chunked_data << data_array[0]
data = data_array[1]
if @chunked_data =~ /(\d{3})(.*)<(.*)>##(.*)##/
dispatch_data
@chunked_data = ""
end
end
end
That is used in several classes as instance methods. Now, I want to
include this method in a module to be DRY. And make this method
available to classes that mixin that module.
The only trouble is, it uses a instance_variable.
Of course…i can keep this method as it is. assuming all the consumer
classes will have their this particular instance variable with same
name.
The other approach could be, as suggested by Eric H. on IRC, is to
have some kind of include, that hookes correct instance variable to
the module upon inclusion in the class.
And this bit…almost escaped me.
If i use set_instance_variable or something like that, then still i
won’t be able to know the name of that particular instance variable
,right?
if i pass the @chunked_data from the any class to the parse_data
method, i would recieve it as a local variable in method and any
changes to the varible will be lost.
Why can’t we have something like C++ reference, where if i modify the
variable in mixed in method, i would effectively change the instance
variable?
Not even that, although in Ruby, AFAIK(which is very little, to tell
the truth) values are passed by reference, even if we pass a local
variable to the parse_data
method and do some modification and try to read back the data in
calling method, changes will be lost.