lt operator on class object, checking a class' superclasses

T

Tomasz Wrobel

Hi!

Do you know a good way to check whether a class is a subclass of a
certain class (given the class object, not an instance)?

one way would be to check all of klass.superclass,
klass.superclass.superclass till nil

or klass.new.is_a?( cklass )

Accidentaly, I tried to such a operation:

klass < cklass

and it seems to work, but doesn't seem to be documented
can somebody acknowledge this?

Best regards,
Tomek W
 
J

Jesús Gabriel y Galán

Hi!

Do you know a good way to check whether a class is a subclass of a
certain class (given the class object, not an instance)?

one way would be to check all of klass.superclass,
klass.superclass.superclass till nil

or klass.new.is_a?( cklass )

Klass.ancestors.include?(Ancestor)

For example:

irb(main):003:0> String.ancestors.include? Object
=> true

Jesus.
 
Q

Quintus

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Am 06.08.2010 10:57, schrieb Tomasz Wrobel:
Hi!

Do you know a good way to check whether a class is a subclass of a
certain class (given the class object, not an instance)?

one way would be to check all of klass.superclass,
klass.superclass.superclass till nil

or klass.new.is_a?( cklass )

Accidentaly, I tried to such a operation:

klass < cklass

and it seems to work, but doesn't seem to be documented
can somebody acknowledge this?

Best regards,
Tomek W


It is documented, although it may be a bit hard to find, since the
behaviour is not defined in the Class class as one might expect, but in
it's superclass, Module. See the Module#<, Module#>, Module#<= and
Module#>= methods on http://www.ruby-doc.org/ruby-1.9/index.html . I'm
not sure what about Module#<=> since the docs say it works on mixins,
but it appears to work on classes as well.

Vale,
Marvin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkxb1O0ACgkQDYShvwAbcNkNmACgjN0UrOwkN3iuhovolkoXjWgM
OWYAoIOPgogYxBm2QHGkFSvmpO181yAe
=/E5l
-----END PGP SIGNATURE-----
 
T

Tomasz Wrobel

It is documented, although it may be a bit hard to find, since the

I see. I got deceived by the fact that the method actually seems to be
defined in Object.

Thanks for answers,
best regards,
Tomek W
 
M

MichaÅ‚ Åomnicki

It is documented, although it may be a bit hard to find, since the
behaviour is not defined in the Class class as one might expect, but in
it's superclass, Module.

What's more '<' operator itself is defined in Object class not in
Module.
Module.methods - Object.methods
=> ["nesting"]

So all in all one expects '<' method to be defined for Class, but it's
defined for Object and documented for Module. Pretty messy ;)
 
C

Calamitas

2010/8/6 Micha=C5=82 =C5=81omnicki said:
Module.methods - Object.methods
=3D> ["nesting"]

Module.instance_methods - Object.instance_methods
=3D> ["private_class_method", "const_missing",
"public_instance_methods", "method_defined?", "included_modules",
"const_get", "module_eval", "class_variables",
"protected_instance_methods", "public_method_defined?", "const_set",
"class_eval", "include?", "private_instance_methods",
"private_method_defined?", "name", "autoload", "<", "<=3D>", ">",
"instance_method", "protected_method_defined?", "const_defined?",
">=3D", "ancestors", "<=3D", "public_class_method", "instance_methods",
"class_variable_defined?", "constants", "autoload?"]

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top