I'm trying to generate a bunch of variables that will be operated on. Started with 2 variables, 4, 8,16 and its about time I put the variable generation in a loop.
The variable is acting as a storage for the index of an array.
So a thread wakes up, picks up a variable and says "variable_0" is the first index, and stores location 24 inside of it.
This is usually done like so. This variable goes on and is used multiple times later on.
variable_0 = get_index(threadid)
Once I have 16 of such variables, its a pain and ugly to see that line repeated more than once. So I thought I'd put in a loop and tried it with 2 methods.
1. EVAL
for i in 0..15 do
eval("variable_#{i} = get_index(threadid)")
end
Does not work at all.
2. instance_variable_set
for i in 0..15 do
index_name = "variable_#{i}"
index_val = get_index(threadid)
instance_variable_set("#{index_name}", :index_val)
end
This doesn't work either, since a later statement prints "variable not found " pretty much.
Is there a good way to go about doing this?
Due to restrictions later down the line, I cannot use an array for this, each variable has to be constructed as a single variable, so no arrays or anything like that would work