Forum: Ruby abstract class

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
91f93d595503b98f18207fc95d66b573?d=identicon&s=25 hochherz (Guest)
on 2005-12-01 18:55
(Received via mailing list)
hey

is there something like abstract classes in ruby?
somehow a class i could not create but wich is a superclass of some
classes.

thanks
olaf
Ec9233451f7c6ba37a83388b87a1f565?d=identicon&s=25 gavin (Guest)
on 2005-12-01 19:20
(Received via mailing list)
This question was also asked about 7 months ago. See the responses in:
http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/rub...
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2005-12-01 19:36
(Received via mailing list)
hochherz wrote:
> hey
>
> is there something like abstract classes in ruby?
> somehow a class i could not create but wich is a superclass of some
> classes.
>
> thanks
> olaf

   class Class
     alias_method :__new__, :new
   end

   module NonInstantiable
     def self.included(klass)
       super
       klass.module_eval do
         def self.new
           raise "Cannot instantiate abstract class #{self}"
         end

         def self.inherited(subklass)
           subklass.module_eval do
             def self.new(*args, &block)
               __new__(*args, &block)
             end
           end
         end
       end
     end
   end

   class BaseKlass
     include NonInstantiable
   end

   class SubKlass < BaseKlass; end

   puts SubKlass.new
   puts BaseKlass.new
Ec9233451f7c6ba37a83388b87a1f565?d=identicon&s=25 gavin (Guest)
on 2005-12-01 19:48
(Received via mailing list)
> is there something like abstract classes in ruby?
> somehow a class i could not create but wich is a superclass of some classes.

You can always 'hide' the new:

irb(main):001:0> class Foo; def self.new; end; end
=> nil
irb(main):002:0> f = Foo.new
=> nil
91f93d595503b98f18207fc95d66b573?d=identicon&s=25 hochherz (Guest)
on 2005-12-01 20:00
(Received via mailing list)
nice solution
45196398e9685000d195ec626d477f0e?d=identicon&s=25 transfire (Guest)
on 2005-12-01 21:14
(Received via mailing list)
What do you use this for?
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2005-12-01 23:28
(Received via mailing list)
hochherz wrote:
 > nice solution

Thank you very much :)

Trans wrote:
> What do you use this for?

I don't. But if I was to use it, that would be the way I'd do it.



Cheers,
Daniel
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 gwtmp01 (Guest)
on 2005-12-01 23:52
(Received via mailing list)
On Dec 1, 2005, at 3:12 PM, Trans wrote:
> What do you use this for?

Sometimes you have an abstract class that models all the common
behavior but
to "complete" the functionality some methods need to be defined/
overridden
in the subclasses.  In this situation, instantiating the abstract
class is
an error since it is only the subclasses that are "complete".  I just
had
a use for this pattern and I decided to just make #new private:

	class Base
	  class <<self; private :new; end
         end

	class Derived < Base
	  class <<self; public :new; end
         end

The advantage to this is you can still have common initialization
code in
Base#initialize but it can only be accessed by calling super from
#initialize in a subclass.

It is similar to how Enumerable doesn't make sense as a class because
it isn't complete without #each being defined.
Ec9233451f7c6ba37a83388b87a1f565?d=identicon&s=25 gavin (Guest)
on 2005-12-02 00:41
(Received via mailing list)
> It is similar to how Enumerable doesn't make sense as a class because
> it isn't complete without #each being defined.

Which is also one thing that makes abstract classes not nearly as
useful in Ruby. Instead of creating an abstract class and inheriting
classes from it, create a module and mix it into the instantiable
classes.
This topic is locked and can not be replied to.