Abstract Classes Or Interfaces

Hi,

Lately I’ve been working with external API’s and when writing tests I’ve
found it extremely useful to have an abstract class (or something like a
java interface) so that I can have an actual implementation for
production and dev, and a dummy one that does not use the real API for
tests. But since ruby does not provide abstract classes, I had to
implement it throw some metaprogramming at it and use it as a mixin.

I think this is clearly an overkill for something that would probably be
useful for many people. Is there any better way to achieve this goal?
And do you think this would be a good feature to add to rails or ruby?

Regards,
Lus Ferreira

“Luís Ferreira” [email protected] wrote in post #1060014:

Lately I’ve been working with external API’s and when writing tests I’ve
found it extremely useful to have an abstract class (or something like a
java interface) so that I can have an actual implementation for
production and dev, and a dummy one that does not use the real API for
tests. But since ruby does not provide abstract classes, I had to
implement it throw some metaprogramming at it and use it as a mixin.

You seem to be thinking like a Java programmer (or at least your brain
is stuck in the land strict static typing). It’s not that Ruby “doesn’t
provide abstract classes,” but rather Ruby has no need strict dependence
on the concept of abstract classes.

Ruby is a dynamically typed language and isn’t bound by the restrictions
of statically typed languages. I’m not necessarily saying that static
typing is bad, I’m just saying that Ruby plays by a different set of
rules. Rules that give developers a lot more freedom IMHO.

I think this is clearly an overkill for something that would probably be
useful for many people. Is there any better way to achieve this goal?
And do you think this would be a good feature to add to rails or ruby?

You are solving an already solved problem. There are a number of mocking
frameworks to handled exactly what you’re attempting to do on your own.

Here’s just one example:
http://gofreerange.com/mocha/docs/