From: Logan Capaldo Date: 2007-05-07T03:53:12+09:00 Subject: Re: dynamically named variables or Constants ------=_Part_60482_22994346.1178477590209 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 5/6/07, Tom V. wrote: > > "Logan Capaldo" wrote: > > > > You guess is in the territory, but it's not really about session's > > capability. if xyz_method is a controller action for instance, and > > abc_method is a controller action, there's nothing guaranteeing per se > > that you're state is going to be persisted between user interactions > > with the web app. What you really need to store in session is the name > > of the constant, AND it's value. and then you need to const_set it at > > the beginning of every method where you make use of this live tree > > thing. > > > > Hi, > Creating a new constant from session data still doesn't work. > As you said I now store both constant_name and constant_value in session. > > However, when trying to test the new Constant (const_defined?), > I get a NameError (see below). > > Also, when printing it, its value is printed, not its name: > "{@new_constant}" --> "/path/to/nirvana". > How do I access its name? > > Maybe you find an error in code. > Or maybe you know of another way > to create dynamically named variables..? > > > class AbcController < ApplicationController > > before_filter :get_constants, :except => :define_constants > > def define_constants > @user = "tom" > constant_name = "#{@user.upcase}_PATH" > logger.info("#{constant_name}") > # output: TOM_PATH > > constant_value = self::class::const_set(constant_name, > "/path/to/nirvana") > logger.info("#{constant_value}") > # output: /path/to/nirvana > > session[:constant_name] = constant_name > session[:constant_value] = constant_value > end > > def get_constants > if session[:constant_name] > @constant_name = session[:constant_name] > logger.info("#{@constant_name}") > # output: TOM_PATH > > if session[:constant_value] > @constant_value = session[:constant_value] > logger.info("#{@constant_value}") > # output: /path/to/nirvana > > @new_constant = self::class::const_set(@constant_name, > @constant_value) > logger.info("#{@new_constant}") > # output: /path/to/nirvana > > evaluate_constant = self::class::const_defined?(@new_constant) should be self::class:const_defined?(@constant_name) of course. @new_constant is gonna hold the value of the constant, not the name. # output: NameError (wrong constant name /path/to/nirvana) > > end > end > end > > end > > Thanks for your input, > Tom. > > ------=_Part_60482_22994346.1178477590209--