From: "Peña, Botp" Date: 2008-07-01T19:25:29+09:00 Subject: Re: Dynamic Variables From: Marc Heiler [mailto:shevegen@linuxmail.org] # Is there any way in ruby to create dynamic variables? note sure, but no. There's no need ;) # counter = 52 # box#{counter} = "cat and dogs" # There would be then a new variable box52 # Note that this is not a question whether someone should do it or # not -this is solely whether it is doable or not. Until today I # thought it is somehow possible with eval but I failed, so I assume # it is not possible. variables in ruby are just references to objects. and since ruby objects can be dynamic, variables are dynamic in a basic sense, but in an o-o way ;) on your case, why not make box refer to a dynamic object, like a hash? eg, box={} #=> {} counter = 52 #=> 52 box[counter] = 'cat and dogs' #=> "cat and dogs" box[counter] #=> "cat and dogs" store anything you want, box["myname"]="botp" #=> "botp" even store a proc and call it box[42]=lambda{box[counter].upcase} #=> # box #=> {"myname"=>"botp", 52=>"cat and dogs", 42=>#} box[42].call #=> "CAT AND DOGS" and you can even let box, cleanup itself :) box["cleanup"]=lambda{box.clear} #=> # box["cleanup"].call #=> {} box #=> {} kind regards -botp