It appears that const_defined? only returns true if the class is the
originator of the constant. If the constant was borrowed from
somewhere else (in this case, File::Constants), it returns false.
That’s on purpose, right? If that’s the case, I must resort to
IO.constants.member?(‘SYNC’) for my intended behavior, I suppose.
Seems a little strange.
At Mon, 16 Jan 2006 17:05:29 +0900,
Daniel A. wrote in [ruby-talk:175899]:
Is this the intended behavior of const_defined?
IO::SYNC
=> 4096
IO.const_defined? :SYNC
=> false
It appears that const_defined? only returns true if the class is the
originator of the constant. If the constant was borrowed from
somewhere else (in this case, File::Constants), it returns false.
Yes.
That’s on purpose, right? If that’s the case, I must resort to
IO.constants.member?(‘SYNC’) for my intended behavior, I suppose.
Seems a little strange.
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.