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?
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.
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
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 2/28/08, Florian G. [email protected] 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:
-
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. -
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. -
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.denhaven2.com/