Serhii Potapov
Serhii Potapov

Reputation: 3990

Why Ruby doesn't support abstract classes?

Sorry,if it's a duplication. I found a plenty of posts about HOW to implement abstract classes, but any about WHY they are not implemented in Ruby.

I work with Ruby about 4 years. And I really still don't understand why Ruby doesn't have a native implementation of abstract classes? Sometime it's really necessary for a good application design. Me and other developers do again and again empty method with "NoImplementedError" but I don't feel OK about it.

Is there a tricky ideology behind to not support interfaces and abstract classes?

Upvotes: 2

Views: 1036

Answers (1)

Anton
Anton

Reputation: 2483

If you are considering abstract classes or interfaces, you are most likely thinking of creating some kind of contract to your code.

However, Ruby is designed to be a weakly-typed is not designed to be a statically typed language and does rely heavily on duck typing. Obviously, it might be really useful in some cases to perform an interface check (to ensure the passed object would support all needed methods, for instance), but it is still will be done at run time, rendering the feature practically useless.

As far as I remember, there was an intention to create a typed version or Ruby and Dave Thomas even mentioned a person who tried this and told that it is not working out well :)

Upvotes: 3

Related Questions