Daniel Amelang
1/17/2006 3:34:00 AM
Pretty cool!
Not what I was looking for, but may prove useful later.
Dan
On 1/16/06, Logan Capaldo <logancapaldo@gmail.com> wrote:
>
> 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
>
>
>
>