Alle domenica 6 maggio 2007, Tom V. ha scritto:
> 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.

I can't understand why you get an error. I modified your code just a bit (by 
the way, what exactly is session?) to give the two methods some context and 
put it in a script:

class MyClass

  def initialize
    @user = 'tom'
    @session = {}
  end

  def xyz_method
  
      constant_name = "#{@user.upcase}_VAR"
  
      constant = MyClass.const_set(constant_name, "/path/to/nirvana")
      
      @session[:constant_name] = constant_name
  
  end
  
  def abc_method
  
      @constant = MyClass.const_get(@session[:constant_name])
      puts @constant
  
  end
end

c = MyClass.new
c.xyz_method
c.abc_method

This works. Since I don't know anything about rails and the live tree plugin, 
I may have missed something related to them. You can try using 
MyClass.constants in the abc_method to get a list of the constants defined 
for MyClass; this could give you some hints about what's happening.

Stefano