From: Joel VanderWerf Date: 2002-08-08T13:53:08+09:00 Subject: Re: how do i mock the presence / absence of a constant w/out getting warnings? Patrick May wrote: > I need to write tests for some behavior that needs to check a > constant: > > if (MOD_RUBY) > .... do stuff > else > .... do something else > end > > I can do it by having this sort of thing: > > MOD_RUBY = true > > and > > MOD_RUBY = false > > but I don't want to get the ruby warnings. What's another way for me > to mock this constant? Not sure I understand the problem, but if you're trying to redefine a constant in a way that won't generate warnings, how about using modules? module M1; K = 1; end module M2; K = 2; end # etc., for as many times as you need to change K include M1 # now K == 1 include M2 # now K == 2 But repeating 'include M1' doesn't go back to K == 1. Ruby checks to see if the module has already been included. If you need to be able to change the constant more dynamically, you can do something like: m = Module.new m.const_set(:K, variable) include m