abstract class

H

hochherz

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
 
P

Phrogz

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
 
D

Daniel Schierbeck

hochherz said:
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
 
H

hochherz

nice solution

Daniel said:
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
 
G

gwtmp01

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.
 
P

Phrogz

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,197
Messages
2,571,041
Members
47,643
Latest member
ashutoshjha_1101

Latest Threads

Top