2

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?

Sergey Potapov
  • 3,819
  • 3
  • 27
  • 46
  • Found similar post: http://stackoverflow.com/questions/5076933/interfaces-abstract-classes-unnecessary-in-ruby-can-someone-explain – Sergey Potapov Nov 09 '12 at 15:28

1 Answers1

3

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 :)

Anton
  • 2,483
  • 2
  • 23
  • 35
  • Ruby is not weakly typed ... it's just not statically typed : http://en.wikipedia.org/wiki/Strong_typing#Variation_across_programming_languages – Anthony Alberto Nov 09 '12 at 16:12
  • Well, yes, a good point: it is neither weak typed, nor strong typed – fixed the answer. – Anton Nov 09 '12 at 16:14
  • 1
    It's strong, dynamic typed and greedy evaluated, object-oriented scripting language. – Hauleth Nov 09 '12 at 16:26
  • It is not strong typed in terms of type checks, so it is arguable – cannot invent any kind of type check which would be strictly placed and unavoidable. – Anton Nov 09 '12 at 16:32
  • strong typing != static typing ... this often is a debate but for me, ruby is strongly typed. – Anthony Alberto Nov 09 '12 at 17:06
  • I understand your point, but if we follow the definition of strong typing (saying the programming language is checking types, no matter at compile time or run time) the way ruby does it falls out a bit: the language itself does not perform any type checks, just you code can throw exceptions here or there if a method gets called with something you don't like. However, it is also a kind of type checking anyway. So I could not agree it is *purely* strongly typed as @Haleth advocates. – Anton Nov 09 '12 at 17:09
  • There is a lof of code when people relies on theire own hached abstract classes like ActiveRecord::Base. So I think supporting something like abstract classes would be useful. For example [Mirah](http://en.wikipedia.org/wiki/Mirah_%28programming_language%29) supports both dynamic and static typing. – Sergey Potapov Nov 09 '12 at 18:17
  • Well, in terms of "types which cannot be instantiated" – right, it would be useful in some cases... – Anton Nov 09 '12 at 18:26