Forum: Ruby const_defined? behavior

6a480fcc49315c993fc2c4f37c882133?d=identicon&s=25 Daniel Amelang (Guest)
on 2006-01-16 09:07
(Received via mailing list)
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.

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.

Dan Amelang
04a56914cc09f0858d3fca2bf4cbde34?d=identicon&s=25 nobuyoshi nakada (Guest)
on 2006-01-16 09:47
(Received via mailing list)
Hi

At Mon, 16 Jan 2006 17:05:29 +0900,
Daniel Amelang 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.

defined?(IO::SYNC)
6a480fcc49315c993fc2c4f37c882133?d=identicon&s=25 Daniel Amelang (Guest)
on 2006-01-17 01:09
(Received via mailing list)
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
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-01-17 01:24
(Received via mailing list)
On Jan 16, 2006, at 5:52 PM, Daniel Amelang wrote:

>
> # 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
6a480fcc49315c993fc2c4f37c882133?d=identicon&s=25 Daniel Amelang (Guest)
on 2006-01-17 05:10
(Received via mailing list)
Pretty cool!

Not what I was looking for, but may prove useful later.

Dan
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.