Isaac Sanders wrote in post #1020785:
> you could use eval, passing in the string.
Beware that eval can execute *any* ruby code, which in turn can execute
*any* shell code, e.g. via system(), so is dangerous if the data could
have come from an untrusted source.
Normally I would recommend using send() for something like this:
meth = :showURL # dynamic method name in variable
obj.send(meth) # call that method
However what's dynamic in your case is that you're trying to pick one of
12 local variables called "t1" to "t12".
This is almost certainly the wrong design, as local variables aren't
really supposed to be used in this way.
I'm guessing that earlier on you had 12 lines of code to assign to those
12 variables? What you probably want is an Array or Hash instead. Then
(a) you can create those 12 instances in better ways (e.g. in a loop);
and
(b) you can do your lookup directly without any eval tricks:
t = []
t[0] = ...
t[1] = ...
... later ...
(0..11).each do |i|
t[i].showURL
end
Then you'll find there's an even cleaner way to iterate:
t.each do |elem|
elem.showURL
end
--
Posted via http://www.ruby-forum.com/.