From: Tomasz Wrobel on 6 Aug 2010 04:57 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/.
From: Jesús Gabriel y Galán on 6 Aug 2010 05:15 On Fri, Aug 6, 2010 at 10:57 AM, Tomasz Wrobel <tomekwr(a)gmail.com> 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.
From: Quintus on 6 Aug 2010 05:25 -----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-----
From: Tomasz Wrobel on 6 Aug 2010 06:49 > 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/.
From: Michał Łomnicki on 6 Aug 2010 06:53 > 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/.
|
Next
|
Last
Pages: 1 2 Prev: Connecting to SQL Server 2005 Next: regular expression negate a word (not character) |