From: Xavier Noria Date: 2012-03-22T14:56:16+09:00 Subject: Re: What’s the best way to check if a feature/class has been loaded? --047d7b15b103d87a6d04bbce90d7 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Thu, Mar 22, 2012 at 6:15 AM, Nikolai Weibull wrote: I mean that if const_defined? returns true, then between that code > being executed and the call to const_get, some other piece of code may > remove the constant, resulting in an uncaught NameError. > That's true. > The problem with const_get is that it follows the ancestors, of course, > and > > you have no way around that in 1.8. > > Yes, we=E2=80=99ve already come to that conclusion. You don=E2=80=99t ne= ed to keep > repeating it. > Because the proposed solutions are based on const_get, and they fail for some inputs (in my interpretation of failure, which may not be yours). I see that you completely cut out the part about const_defined? not > calling const_missing, which is a rather big part of the problem with > using const_defined? in the first place. > We are supposedly emulating defined? somehow but for constant paths and without going up the ancestor chain in each step doesn't it? defined? does not call const_missing. > Nikolai, I don't really understand what you need. > > Xavier, please don=E2=80=99t begin sentences with the person you=E2=80=99= re responding > to=E2=80=99s name like that. It=E2=80=99s like your speaking to me as if= I was a > child and it feels very condescending. > Sorry, it was not my intention to sound like that. > And you refuse to reuse much more simple and proven existing solutions. > > You=E2=80=99re making it sound like I=E2=80=99m being stubborn. I just d= on=E2=80=99t think > your proposed solution is better. > The problem is that you don't really want qualified_const_defined?, you need something else but I don't see clearly what it is. I want a way to tell if a class or module has been defined/loaded. > The check would then be to see if X was loaded in your example above, > not X::Y. > I don't follow, in order to see if X::Y is a defined constant I need first to check if X is a defined constant? Constants have single names "X", and "Y". X::Y is a constant path. "X::Y" as such does not exist. An alternative is to check $LOADED_FEATURES. This isn=E2=80=99t > straightforward either, as it doesn=E2=80=99t contain the exact argument = given > to require. There are internal functions like rb_provided that could > have been exposed to make it easy to check if a feature had been > loaded/is available. > File names and class objects, and module objects, and constants... you cannot derive one from the other. They are decoupled in Ruby except for the fact that the class/module keywords assign, and that if you assign an anonymus class/module to a constant, then its name is set after the constant. But in Ruby file foo.rb can define the constant Bar, which may hold a module whose name is "Wadus". They are quite orthogonal features. --047d7b15b103d87a6d04bbce90d7 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Thu, Mar 22, 2012 at 6:15 AM, Nikolai Weibull <now@bitwi.se> w= rote:

> The problem with const_get is that it follows the ancestors, of course= , and
> you have no way around that in 1.8.

Yes, we=E2=80=99ve already come to that conclusion. =C2=A0You don=E2= =80=99t need to keep
repeating it.

Because the proposed solu= tions are based on const_get, and they fail for some inputs (in my interpre= tation of failure, which may not be yours).

=C2=A0= I see that you completely cut out the part about const_defined? not
calling const_missing, which is a rather big part of the problem with
using const_defined? in the first place.

We are supposedly emulating defined? somehow but for constant paths and w= ithout going up the ancestor chain in each step doesn't it? defined? do= es not call const_missing.


=C2=A0> Nikolai, I don't really u= nderstand what you need.

Xavier, please don=E2=80=99t begin sentences with the person you=E2= =80=99re responding
to=E2=80=99s name like that. =C2=A0It=E2=80=99s like your speaking to me as= if I was a
child and it feels very condescending.

= Sorry, it was not my intention to sound like that.


> And you refuse to reuse much more simple and proven existing solu= tions.

You=E2=80=99re making it sound like I=E2=80=99m being stubborn. =C2= =A0I just don=E2=80=99t think
your proposed solution is better.

The p= roblem is that you don't really want qualified_const_defined?, you need= something else but I don't see clearly what it is.


I want a way to tell if= a class or module has been defined/loaded.
The check would then be to see if X was loaded in your example above,
not X::Y.

I don't follow, in order = to see if X::Y is a defined constant I need first to check if X is a define= d constant? Constants have single names "X", and "Y". X= ::Y is a constant path. "X::Y" as such does not exist.


An alternative is to check $LOADED_FEATURES. =C2=A0This isn=E2=80=99t
straightforward either, as it doesn=E2=80=99t contain the exact argument gi= ven
to require. =C2=A0There are internal functions like rb_provided that could<= br> have been exposed to make it easy to check if a feature had been
loaded/is available.

File names and cla= ss objects, and module objects, and constants... you cannot derive one from= the other. They are decoupled in Ruby except for the fact that the class/m= odule keywords assign, and that if you assign an anonymus class/module to a= constant, then its name is set after the constant.

But in Ruby file foo.rb can define the constant Bar, wh= ich may hold a module whose name is "Wadus". They are quite ortho= gonal features.

--047d7b15b103d87a6d04bbce90d7--