From: Stefano Crocco Date: 2007-05-06T20:21:53+09:00 Subject: Re: dynamically named variables or Constants Alle domenica 6 maggio 2007, Tom V. ha scritto: > Hi, > > for each user I need to create and call > a dynamically named _GLOBAL_ variable (or Constant) > labeled with the user name. > To make it clear what I mean: > > # begin "Ruby Pseudocode" > user_name = "tom" > $#{user_name}_dyn_var = "/path/to/nirvana" > # end "Ruby Pseudocode" > > Is there a way to do that? > How would I call that new variable '$#{user_name}_dyn_var' ? > I tried "Module::const_set": > > # begin Ruby > constant_name = "#{user_name.upcase}_VAR" > constant = self::class::const_set(constant_name, "/path/to/nirvana") > # end Ruby > > but couldn't find a way to call "#{user_name.upcase}_VAR". > > you may ask why I either need a global var or a Constant: > I am using the "live_tree" plugin > from http://www.epiphyte.ca/code/live_tree.html > to create a file system browser. > As far as I can tell, > the plugin requires either a global var or a Constant > to initialize the file system model. > > If someone has tried this plugin before > and found another solution, let me know. > The author is not responding to my mails. > > Thanks for your input, > Tom. I'm not sure about what you mean when saying 'a way to call "#{user_name.upcase}_VAR". If you mean how to get the value of the constant, then you can use const_get, which works like const_set: self.class.const_get( constant_name) => "/path/to/nirvana" I hope this helps Stefano