On Sun, May 16, 2004 at 04:03:50AM +0900, Dirk Einecke wrote:
> Hi.
> Is there a faster way for example with a loop to write all given
> parameters in local vars?
Well, you could do something with eval allong the following lines, but
It's a rather Bad Idea.
def setup(params, binding)
params.each do |key, val|
eval("%s = %s" % [key, val[0]], binding)
end
end
# the call to Kernel#binding method is the key here, since it allows you
# to assign to the variables within the correct scope.
setup(cgi.params, binding)
A cleaner solution might well be to use a proxy object, which makes use
of the special method, method_missing to map keys to values.
class CgiParam
def initialize(params)
@params = params
end
def method_missing(m, *other)
@params[m.to_s][0]
end
end
p = CgiParam.new(cgi.params)
.. if (p.foo) # do things
... unless (p.bar.to_i) # other things
Although there may still be a better way of doing it.
--
Ceri Storey <>
|