From: Logan Capaldo Date: 2006-01-17T08:09:30+09:00 Subject: Re: const_defined? behavior On Jan 16, 2006, at 5:52 PM, Daniel Amelang wrote: > Hi Nobu, > >> defined?(IO::SYNC) > > In my case, the constant's name is stored in a variable. So, I'd have > to resort to doing an 'eval' if I were to follow your suggestion. So > at least I have two options now: > > # Option 1 (Nobu's suggestion) > found = eval("defined?(obj::#{const})") > > # Option 2 (My original idea) > found = obj.respond_to?(:constants) && obj.constants.member?(const) > > Any better ideas? I usually try to stay clear of 'eval' if I can. > > Dan Amelang > def const_defed(var) var.split(/::/).inject(Object) do |left, right| begin left.const_get(right) rescue NameError break nil end end end