Hi-
I've done some searching on this topic and it seems to be generally
discouraged to use one variable's value as another variable's name (or
part of it's name). People generally recommend a hash or array to answer
this use case.
I've seen these posted as "available but not recommended":
eval("#{variable_name} = #[value}")
or:
self.instance_variable_set(:@var_name, value)
or:
x="myvar"
myvar="hi"
eval(x) -> "hi"
I'm trying to write a short program and I'm thinking that I'm going in
the wrong direction - can anyone give me some fresh ideas as an
alternative to what I see as my "best solution?"
I have string1, stringA (which won't be known till later) that is
associated with string1, and then stringB and potentially stringC and
stringD which are all associated with string1 and stringA.
So I'm thinking that I want a hash, big_hash, with string1 as the key,
and the value is another hash, string1_hash, with stringA as the key and
the value is an array containing stringB,stringC,stringD.
I realize I could name string1_hash anything, but I'm going to need
multiple hashes as I iterate over a list of string1, string2, string3.
I'd like big_hash to be somewhat permanent so I can re-use it elsewhere.
I'm not looking for someone to solve the problem for me, but really just
for other ideas beyond my clunky-semi-permanent data structure. Do I
need to store these associated values in a database, rather than trying
to build one out of these hashes? Is my direction of making big_hash
semi-permanent a bad idea, and I should just keep re-using big_hash over
and over as I iterate through my list of string1, string2, string3? As
I'm asking the question I think that is the direction I'm going to try
next, but I would still appreciate advice on this topic.
TIA,
Peter
···
--
Posted via http://www.ruby-forum.com/.