M
Matt Brooks
I have a function write_log that takes in a string and it prints to
console and to a logger.
I need to loop through all variables found in an index of an array as a
hash and print them. This list of variables to iterate through is of
variable length(num_bins). The list has variable names(hash keys) that
were created by incrementing the number at the end of the variable name.
There is a little more to the problem in the fact that the signals array
contains objects which have a method_missing defined to handle these
variables by looking them up in a hash. So when signals[0].bin_0 is
called, it looks in the object of the first index of the signals array
for a method called bin_0, it doesn't exist, so I defined the code in
the method_missing method to simply look up that name, bin_0, in a hash
and return that value.
Problem is when I create this variable with an incrementing name, I
can't seem to append it to the end of the signals[0], because I can't do
#{} with another #{} inside of it. I would like this:
write_log("Bin_\##{index}: #{signals[0].#{variable}}")
How can I accomplish this?! Dereference a variable within a variable
that is being dereferenced.
index = 0
while index < num_bins
variable = "bin_#{index}".to_sym
write_log("Bin_\##{index}: #{signals[0].#{variable}}")
index += 1
end
SHOULD:
create calls to
signals[0].bin_0
signals[0].bin_1
signals[0].bin_2 etc until num_bins reached...
Thanks a lot for your help,
Matt
console and to a logger.
I need to loop through all variables found in an index of an array as a
hash and print them. This list of variables to iterate through is of
variable length(num_bins). The list has variable names(hash keys) that
were created by incrementing the number at the end of the variable name.
There is a little more to the problem in the fact that the signals array
contains objects which have a method_missing defined to handle these
variables by looking them up in a hash. So when signals[0].bin_0 is
called, it looks in the object of the first index of the signals array
for a method called bin_0, it doesn't exist, so I defined the code in
the method_missing method to simply look up that name, bin_0, in a hash
and return that value.
Problem is when I create this variable with an incrementing name, I
can't seem to append it to the end of the signals[0], because I can't do
#{} with another #{} inside of it. I would like this:
write_log("Bin_\##{index}: #{signals[0].#{variable}}")
How can I accomplish this?! Dereference a variable within a variable
that is being dereferenced.
index = 0
while index < num_bins
variable = "bin_#{index}".to_sym
write_log("Bin_\##{index}: #{signals[0].#{variable}}")
index += 1
end
SHOULD:
create calls to
signals[0].bin_0
signals[0].bin_1
signals[0].bin_2 etc until num_bins reached...
Thanks a lot for your help,
Matt