Philip, if i understand you correctly:
my word! i have been trying to do something similar to this for a long
time, (you may have notice a couple of other threads) and never could
figure it out. but then the other day i figured how to create class
variables (@@x) and put them in modules using a module methods to set
them and including them into my classes. this allowed me to pass a
variable to vastly disperate objects. awsome.
but then i looked over your problem. and recalled trying to do the same
kind of thing when i first started my cgi app. why? probably like you, i
wanted to dynamiclly create objects corresponding to my cgi parameters.
and guess what? you got it:
params.each do |param|
Mod.export %w(name email phone)
@name = “me”
@email = “me.com”
@phone = "555-5555"
a = AKlass.new
shouldn’t be too far a cry to integrate in with cgi.
now, i’m thinking i should go back and redo my whole
shabang…ummm…nah, i opted for an cgi <-> xml <-> sql library i
wrote instead. it was a tough task, but works nicely.
On Tue, 2002-07-09 at 18:10, Philip Mak wrote:
I’m trying to make a function that is called like this:
cgi.export(%w(name email phone))
which would have the same effect as this:
name = cgi['name']
email = cgi['email']
phone = cgi['phone']
Since there doesn’t seem to be a way to get the binding of the caller
automatically, I had to try stuff like:
cgi.export(%w(name email phone), binding())
# cgi.export uses the binding to eval
eval cgi.export(%w(name email phone))
# cgi.export returns code to set the variables
But I can’t get it to work. Apparently, it doesn’t seem to be possible
to create a variable in this fashion. Observe the following piece of
code, which looks like it should work but doesn’t:
$ ruby -e"eval ‘x = 3’; puts x"
-e:1: undefined local variable or method `x’ for #Object:0x401ddce0 (NameError)
Does anyone have ideas on how I could create such a function, or am I
trying to do the impossible? I want to minimize the amount of code
that has to be typed by the caller.
“They that can give up essential liberty to obtain a little
temporary safety deserve neither liberty nor safety.”
– Benjamin Franklin