[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

const_defined? behavior

Daniel Amelang

1/16/2006 8:05:00 AM

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


5 Answers

gmurray

1/16/2006 8:39:00 AM

0

% cat io2.rb
#!/usr/bin/env ruby
puts File::SYNC.class
IO::WHY = 56 # define a constant for IO
puts IO.const_defined?(:WHY)
% ruby io2.rb
Fixnum
true

IO::SYNC was not a constant.

regards,
Gerald

Nakada, Nobuyoshi

1/16/2006 8:46:00 AM

0

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)

--
Nobu Nakada


Daniel Amelang

1/16/2006 10:53:00 PM

0

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


Logan Capaldo

1/16/2006 11:10:00 PM

0


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




Daniel Amelang

1/17/2006 3:34:00 AM

0

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
>
>
>
>