Abstract methods in ruby ?

C

cypher.dp

Hi everybody !

How do you implement something like the "abstract" keyword in Java ?
Or: Is there a technique to force a subclass to implement a special method ?

Kind regards

Dominik
 
J

James Edward Gray II

How do you implement something like the "abstract" keyword in Java ?
Or: Is there a technique to force a subclass to implement a special
method ?

One way is:

class Parent
def abstract_method
raise NotImplementedError
end
end

You may just want to skip this step altogether though, since an
exception toss is the default behavior.

Welcome to the word of dynamic typing. ;)

James Edward Gray II
 
C

cypher.dp

Thanks a lot for the warm welcome, James :)




2007/7/12 said:
One way is:

class Parent
def abstract_method
raise NotImplementedError
end
end

You may just want to skip this step altogether though, since an
exception toss is the default behavior.

Welcome to the word of dynamic typing. ;)

James Edward Gray II
 
T

Thomas Hafner

Or: Is there a technique to force a subclass to implement a special method ?

Alternatively try something similiar to the the NVI idiom in C++:

# interface
module MyInterface
def foo(*args, &block)
do_foo(args, block)
end
end

# special implementation of interface
class MyClass
include MyInterface

private
def do_foo(args, block)
# TODO complete implementation
end
end

Regards
Thomas
 

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

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,311
Members
47,986
Latest member
ColbyG935

Latest Threads

Top