abstract_type icon indicating copy to clipboard operation
abstract_type copied to clipboard

Add a method for detecting an abstract class

Open chewi opened this issue 9 years ago • 2 comments
trafficstars

You can't do Foo.include?(AbstractType) because subclasses return true as well. Without such a method, the only other way is this, which isn't very nice.

begin
  Foo.new
rescue NotImplementedError
  true
else
  false
end

chewi avatar Aug 04 '16 15:08 chewi

This sort of sounds like an anti-feature. Could you give an example of why you need to check if a class is abstract?

backus avatar Aug 04 '16 20:08 backus

I have a script that can create instances of various kinds of customer and sometimes the script cannot automatically determine which kind of customer to use so you have to specify. I need to ensure that the type specified is valid, i.e. not abstract. While I agree that this wouldn't be appropriate in a language like C++, classes are first class objects in Ruby and dynamic typing means that you sometimes need to make a check like this.

chewi avatar Aug 04 '16 20:08 chewi