# Abstract superclasses

**URL:** <https://www.ruby-forum.com/t/abstract-superclasses/117535>\
**Category:** Ruby\
**Created:** [October 17, 2007, 9:23pm UTC](https://www.ruby-forum.com/t/abstract-superclasses/117535 "2007-10-17T21:23:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [October 17, 2007, 9:23pm UTC](https://www.ruby-forum.com/t/abstract-superclasses/117535/1 "2007-10-17T21:23:41Z")

</div>

I sometimes use the Java thing of an abstract superclass in Ruby. I  
usually just set up the abstract parent so that it’ll break if used  
directly. Is it better to just use Modules? Have other people run into  
this?

–  
Giles B.

Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
Tumblelog: [http://giles.tumblr.com/](http://giles.tumblr.com/)

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [October 17, 2007, 9:42pm UTC](https://www.ruby-forum.com/t/abstract-superclasses/117535/2 "2007-10-17T21:42:26Z")

</div>

On 17/10/2007, Giles B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I sometimes use the Java thing of an abstract superclass in Ruby. I  
> usually just set up the abstract parent so that it’ll break if used  
> directly. Is it better to just use Modules? Have other people run into  
> this?

I’d use a Module, yes. Or a class:

class SomeClassMimickingAbstract  
private\_class\_method :new  
end

But you don’t need to think this way in Ruby – unlike Java, Ruby is  
weakly typed.

– Thomas A.

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [October 17, 2007, 9:45pm UTC](https://www.ruby-forum.com/t/abstract-superclasses/117535/3 "2007-10-17T21:45:57Z")

</div>

On 17.10.2007 21:22, Giles B. wrote:

> I sometimes use the Java thing of an abstract superclass in Ruby. I  
> usually just set up the abstract parent so that it’ll break if used  
> directly. Is it better to just use Modules? Have other people run into  
> this?

Why bother? If your “abstract” class requires a method to be present  
then just not defining it works pretty well - in modules as well as in  
classes:

irb(main):001:0\> o=Object.new.extend Enumerable  
=\> #Object:0x1001a92c  
irb(main):002:0\> o.map {|y| y.to\_i}  
NoMethodError: undefined method `each' for #<Object:0x1001a92c> from (irb):3:in`map’  
from (irb):3  
from :0  
irb(main):003:0\>

Of course you need some documentation about the missing method - that  
would probably be the only reason why I’d add a dysfunctional definition  
of that method…

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [October 18, 2007, 2:19am UTC](https://www.ruby-forum.com/t/abstract-superclasses/117535/4 "2007-10-18T02:19:06Z")

</div>

]On Oct 17, 2:41 pm, “Thomas A.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> private_class_method :new
> 
> ```
> 
> end
> 
> But you don’t need to think this way in Ruby – unlike Java, Ruby is  
> weakly typed.
> 
> – Thomas A.

How do you suggest to use that class? I understanding keeping the  
“abstract” class from being instantiated, but what about the concrete  
classes, the ones you’re going to use?

class RealThing \< SomeClassMimickingAbstract

# now make .new public again

end

Ugly, isn’t it? Of course, that’s just more fuel for the “don’t do  
this in Ruby” fire.

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [October 18, 2007, 3:40am UTC](https://www.ruby-forum.com/t/abstract-superclasses/117535/5 "2007-10-18T03:40:21Z")

</div>

Hi –

On Thu, 18 Oct 2007, Yossef M. wrote:

> > class SomeClassMimickingAbstract  
> > classes, the ones you’re going to use?
> 
> class RealThing \< SomeClassMimickingAbstract
> 
> # now make .new public again
> 
> end
> 
> Ugly, isn’t it? Of course, that’s just more fuel for the “don’t do  
> this in Ruby” fire.

You could do:

class C  
private\_class\_method :new  
def self.inherited©  
class \<\< c; public :new; end  
end  
end

I’ve actually never felt the need for abstract classes in Ruby  
programs. I’m not a Java programmer, so it never would have occurred  
to me in that context, and I haven’t found myself inventing it for  
Ruby.

David

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [October 18, 2007, 5:31am UTC](https://www.ruby-forum.com/t/abstract-superclasses/117535/6 "2007-10-18T05:31:58Z")

</div>

It is a Java thing; it comes from “Refactoring,” I think, the Martin  
Fowler book. The idea is you handle excessive if/else/etc. statements  
by creating classes and putting the different code branches in  
subclasses. Since you’ll have pieces which don’t need to be different  
in the subclasses, you keep those pieces in the superclass so you have  
them in one place.

In practice I usually do the unimplemented method thing, so that  
misusing the code just makes things blow up. The method hiding and  
revealing is probably overkill, although if you extract it into a  
module, you can have an AbstractSuperclass module pretty easily, which  
is a gajillion times better than comments which say “abstract  
superclass!! do not instantiate!!!”.

As a refactoring it’s generally pretty useful. The code becomes easier  
to test and easier to read. But it’s really not idiomatic at all, and  
it’s very inelegant.

–  
Giles B.

Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
Tumblelog: [http://giles.tumblr.com/](http://giles.tumblr.com/)

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [October 18, 2007, 4:30am UTC](https://www.ruby-forum.com/t/abstract-superclasses/117535/7 "2007-10-18T04:30:01Z")

</div>

On 10/17/07, David A. Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’ve actually never felt the need for abstract classes in Ruby  
> programs. I’m not a Java programmer, so it never would have occurred  
> to me in that context, and I haven’t found myself inventing it for  
> Ruby.

Note that ActiveRecord has a slightly different spin on the notion of  
abstract class.

ActiveRecord::Base has a rw attribute called abstract\_class and an  
abstract\_class? predicate method.

ActiveRecord::Base subclasses can be ‘set’ as abstract the meaning is  
that such classes don’t have a corresponding database table. They are  
used as intermediate superclasses, typically to let a set of AR  
classes to inherit a common database connection different from other  
AR classes in the same application.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![Giles\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/848f3c/32.png) [@Giles\_B](https://www.ruby-forum.com/u/Giles_B)\
**Post date:** [October 18, 2007, 10:52am UTC](https://www.ruby-forum.com/t/abstract-superclasses/117535/8 "2007-10-18T10:52:51Z")

</div>

On 10/18/07, Giles B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
I’d go for modules but Robert is right too, if a class just is  
abstract by the way it is defined or refactored that’s it, no way  
checking it’s instantiation, no please. If you really feel that an  
entity shall not be instantiated than a Module seems to be the correct  
Ruby device.

Cheers  
Robert
