From: m4dc4p Date: 2005-11-15T07:22:16+09:00 Subject: Re: removing a constant definition from an environment Ryan, This is a tricky problem. First, you can't call remove_const directly as it's a private method on the Module class. In the simple case, that means you have to call it from within the class or module from which you want to remove a constant. Further, that call must be made from within a *class* (as opposed to instance) method. Here is a simple program which shows a class which can define and undefine a constant. If you run it with ruby you should see the output in the comments class Foo def self.define_constant class_eval "CONST_VAL = 1" end def self.undef_constant remove_const :CONST_VAL end end puts "CONST_VAL defined?: #{Foo.const_defined?(:CONST_VAL)}" Foo.define_constant puts "CONST_VAL defined now?: #{Foo.const_defined?(:CONST_VAL)}" Foo.undef_constant puts "CONST_VAL still defined?: #{Foo.const_defined?(:CONST_VAL)}" # output: #CONST_VAL defined?: false #CONST_VAL defined now?: true #CONST_VAL still defined?: false There are trickier ways to do this with the "send" method but this be enough should get you started. Robert Evans wrote: > Hi Ryan, > > Thanks for your help. > > I tried that, and it didn't work. Here is my irb transcript trying > that. Is there another way to remove a constant? I thought I had seen > one in the Pickaxe book, but maybe I was thinking of the module method. > > irb > irb(main):001:0> FOO = "my symbol value" > => "my symbol value" > irb(main):002:0> remove_const(FOO) > NoMethodError: undefined method `remove_const' for main:Object > from (irb):2 > irb(main):003:0> remove_const(:FOO) > NoMethodError: undefined method `remove_const' for main:Object > from (irb):3 > irb(main):004:0> ?? > > > Any other ideas? > > Thanks, > Bob Evans > > > On Nov 14, 2005, at 1:44 PM, Ryan Leavengood wrote: > > > On 11/14/05, Robert Evans wrote: > >> > >> I expected the unit test to clean the environment, in lieu of that, > >> how can I undefined const? > > > > ---------------------------------------------------- > > Module#remove_const > > remove_const(sym) => obj > > ---------------------------------------------------------------------- > > -- > > Removes the definition of the given constant, returning that > > constant's value. Predefined classes and singleton objects > > (such as > > _true_) cannot be removed. > > > > You pass it a symbol: remove_const(:VARIABLE) > > > > Ryan > >