From: Xavier Noria Date: 2012-03-22T04:48:15+09:00 Subject: Re: What’s the best way to check if a feature/class has been loaded? --047d7b339d0f79506604bbc61238 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Wed, Mar 21, 2012 at 8:17 PM, Nikolai Weibull wrote: > On Wed, Mar 21, 2012 at 16:43, Xavier Noria wrote: > > Ah, that won't work in 1.8. > > > > In 1.8 you can rely on the fact that const_defined? does not look into > the > > ancestors. I guess a portable solution will need to have code that > depends > > on the Ruby version. > > Or simply use my #feature? implementation. But again, I would like to > use something that I don=E2=80=99t have to copy&paste or require into eve= ry > project where I need this function. Relying on ActiveSupport is > definitely not an option. OK, the implicit answer is that there is no built-in way to do that. You need to write a short method like the ones we are mentioning. Take into account that #feature? assumes the argument matches the constant name, which may not be the case. For example module A module B end end module C D =3D A::B end In that code A::B and C::D are defined constants that store the same module object, but #feature? returns false on C::D. It would be more correct to rely on const_defined? like Active Support does= . --047d7b339d0f79506604bbc61238 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Wed, Mar 21, 2012 at 8:17 PM, Nikolai Weibull <now@bitwi.se> w= rote:
On Wed, Mar 21, 2012 at 16:43, Xavier Noria <fxn@hashref.com> wrote:
> Ah, that won't work in 1.8.
>
> In 1.8 you can rely on the fact that const_defined? does not look into= the
> ancestors. I guess a portable solution will need to have code that dep= ends
> on the Ruby version.

Or simply use my #feature? implementation. =C2=A0But again, I w= ould like to
use something that I don=E2=80=99t have to copy&paste or require into e= very
project where I need this function. =C2=A0Relying on ActiveSupport is
definitely not an option.

OK, the implicit = answer is that there is no built-in way to do that. You need to write a sho= rt method like the ones we are mentioning.

Take in= to account that #feature? assumes the argument matches the constant name, w= hich may not be the case. For example

=C2=A0 =C2=A0 module A
=C2=A0 =C2=A0 =C2=A0 m= odule B
=C2=A0 =C2=A0 =C2=A0 end
=C2=A0 =C2=A0 end

=C2=A0 =C2=A0 module C
=C2=A0 =C2=A0 =C2=A0 = D =3D A::B
=C2=A0 =C2=A0 end

In that cod= e A::B and C::D are defined constants that store the same module object, bu= t #feature? returns false on C::D.

It would be more correct to rely on const_defined? like= Active Support does.


--047d7b339d0f79506604bbc61238--