abstract_type
abstract_type copied to clipboard
Add a method for detecting an abstract class
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
This sort of sounds like an anti-feature. Could you give an example of why you need to check if a class is abstract?
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.