Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > lt operator on class object, checking a class' superclasses

Reply
Thread Tools

lt operator on class object, checking a class' superclasses

 
 
Tomasz Wrobel
Guest
Posts: n/a
 
      08-06-2010
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Jess Gabriel y Galn
Guest
Posts: n/a
 
      08-06-2010
On Fri, Aug 6, 2010 at 10:57 AM, Tomasz Wrobel <> 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.

 
Reply With Quote
 
 
 
 
Quintus
Guest
Posts: n/a
 
      08-06-2010
-----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/

iEYEARECAAYFAkxb1O0ACgkQDYShvwAbcNkNmACgjN0UrOwkN3 iuhovolkoXjWgM
OWYAoIOPgogYxBm2QHGkFSvmpO181yAe
=/E5l
-----END PGP SIGNATURE-----

 
Reply With Quote
 
Tomasz Wrobel
Guest
Posts: n/a
 
      08-06-2010
> 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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Michał Łomnicki
Guest
Posts: n/a
 
      08-06-2010

> 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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Calamitas
Guest
Posts: n/a
 
      08-06-2010
2010/8/6 Micha=C5=82 =C5=81omnicki <>:
>> 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

 
Reply With Quote
 
Michał Łomnicki
Guest
Posts: n/a
 
      08-06-2010
Calamitas wrote:
> 2010/8/6 Michał Łomnicki <>:
>>> Module.methods - Object.methods

>> => ["nesting"]

>
> Module.instance_methods - Object.instance_methods


Of course. Shame on me.

Regards,
Michał
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does compiler only look at public methods of superclasses of...? failure_to@yahoo.co.uk Java 26 01-08-2008 03:19 AM
Exception Superclasses Scott Harper Java 5 05-10-2006 09:30 AM
inherit xdoclet ejb.relation methods from superclasses? fworsley@gmail.com Java 2 08-05-2005 09:53 PM
Finding a class' superclasses Hallvard B Furuseth Python 2 08-23-2004 06:35 PM
Creating / chaining exceptions and inheriting from their superclasses Mark Java 1 04-28-2004 02:49 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57