Lt operator on class object, checking a class' superclasses

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

On Fri, Aug 6, 2010 at 10:57 AM, Tomasz W. [email protected]
wrote:

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.

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

Am 06.08.2010 10:57, schrieb Tomasz W.:

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

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

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

Calamitas wrote:

2010/8/6 MichaÅ‚ Łomnicki [email protected]:

Module.methods - Object.methods
=> [“nesting”]

Module.instance_methods - Object.instance_methods

Of course. Shame on me.

Regards,
Michał

2010/8/6 MichaÅ‚ Łomnicki [email protected]:

Module.methods - Object.methods
=> [“nesting”]

Module.instance_methods - Object.instance_methods
=> [“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”, “<”, “<=>”, “>”,
“instance_method”, “protected_method_defined?”, “const_defined?”,
“>=”, “ancestors”, “<=”, “public_class_method”, “instance_methods”,
“class_variable_defined?”, “constants”, “autoload?”]

Peter

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs