What is the best way to tell if a class some_class is an eigenclass of some object?
What is the best way to tell if a class some_class is an eigenclass
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
(Prior to Ruby 2.0) The following expression evaluates to
trueif an only if the objectxis an eigenclass:The
===equality check asserts thatxis an instance of theClassclass, the!=inequality check uses the fact that theancestorsintrospection method “skips” eigenclasses.For objects
xthat are instances of theObjectclass (i.e.xis not a blank slate object), theClass === xcheck is equivalent tox.is_a? Classor, in this particular case, tox.instance_of? Class.Starting with Ruby 2.0, the above expression is not sufficient to detect eigenclasses since it evaluates to
truealso for classes that haveprepended modules. This can be solved by an additional check thatx.ancestors.firstis not such a prepended module e.g. byClass === x.ancestors.first. Another solution is to modify the whole expression as follows: