[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Abstract class

Ayyanar Aswathaman

2/28/2008 10:55:00 AM

How to create the abstract class and abstract method in ruby.
In RAILS active_record::base there is an attribute abstract_class.
How it is related to the ruby abstract class?
--
Posted via http://www.ruby-....

4 Answers

Stefan Lang

2/28/2008 11:52:00 AM

0

You don't need abstract classes or methods in Ruby.
If you want to share a bunch of methods between
classes, use a module. If the module depends on
other methods not in the module (these would be
abstract methods in a Java class model) say so
in the module documentation.

Ruby's builtin Enumerable module is a good example -
it provides a number of methods that depend only
on an existing "each" method.

Thomas Wieczorek

2/28/2008 1:23:00 PM

0

There is no keyword to create abstract classes, but you can force the
implementation of some methods if you need to.

class Foo
def bar
raise NotImplementedError
end
end

You will only get an exception at runtime, when something tries to
call it. So you should override it in a subclass.

class Bar < Foo
def bar
"Foobar"
end
end

Florian Gilcher

2/28/2008 2:34:00 PM

0

Don't think of this as a "true" abstract class.

It is a value that ActiveRecord uses to determine whether a child of
ActiveRecord::Base is a "true" model (and by that, has a database
table attached to it). This is not a language feature.

Greetings
Skade

On Feb 28, 2008, at 11:54 AM, Ayyanar Aswathaman wrote:

> How to create the abstract class and abstract method in ruby.
> In RAILS active_record::base there is an attribute abstract_class.
> How it is related to the ruby abstract class?
> --
> Posted via http://www.ruby-....
>


Rick DeNatale

2/28/2008 4:13:00 PM

0

On 2/28/08, Florian Gilcher <flo@andersground.net> wrote:
> On Feb 28, 2008, at 11:54 AM, Ayyanar Aswathaman wrote:
>
> > How to create the abstract class and abstract method in ruby.
> > In RAILS active_record::base there is an attribute abstract_class.
> > How it is related to the ruby abstract class?> Don't think of this as a "true" abstract class.
>
> It is a value that ActiveRecord uses to determine whether a child of
> ActiveRecord::Base is a "true" model (and by that, has a database
> table attached to it). This is not a language feature.

Yes, it's very much an ActiveRecord thing. There are three different
uses of building an inheritance hierarcy for ActiveRecord models:

1) For using "single table inheritance" an ActiveRecord class and it's
subclasses all need to map to the same database table. AR does this
by mapping each class to a table based on it's highest non-abstract
ancestor or itself if it's superclass is itself abstract.

2) For providing common setup of multiple tables. One common use of
this is when you have multiple databases, with some tables in one and
some in another. Making an 'abstract' ActiveRecord class for tables
in a particular database to subclass, and setting the abstract class'
database connection is the standard way to do this.

3) Providing common methods. However, this is almost always better
achieved by using Ruby modules rather than inheritance.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...