From: "Tom V." Date: 2007-05-06T21:46:04+09:00 Subject: Re: dynamically named variables or Constants Hi Stefano, thanks for your advice. However, when I try to call "self.const_get" from another method (but same class), I get an error: --> NameError (uninitialized constant MyClass::TOM_VAR) Here is how I created 'TOM_VAR' (simplified): def xyz_method constant_name = "#{@user.name.upcase}_VAR" logger.info("#{constant_name}") # output: TOM_VAR constant = MyClass.const_set(constant_name, "/path/to/nirvana") logger.info("#{constant}") # output: /path/to/nirvana session[:constant_name] = constant_name end In the following method I get the error: def abc_method @constant = MyClass.const_get(session[:constant_name]) # ERROR end Thanks for helping me out Tom. On Sun, 6 May 2007 20:21:53 +0900 Stefano Crocco wrote: > 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 >