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
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
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
On Jul 11, 2007, at 5:40 PM, cypher.dp@gmail.com wrote:
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 ?
cypher.dp@gmail.com wrote/schrieb <2b4059ff0707111540w6cf25d59q10944fad85bc2dd1@mail.gmail.com>:
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
Thanks a lot for the warm welcome, James
2007/7/12, James Edward Gray II <james@grayproductions.net>:
On Jul 11, 2007, at 5:40 PM, cypher.dp@gmail.com wrote:
> 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
endYou 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
> 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
endYou may just want to skip this step altogether though, since an
exception toss is the default behavior.
Hmm, I would say no, you will be caught by #method_missing one day.
It would be interesting why you want to do this?
For documentation purpose? ( Dumbs up !!)
Because you want the feature? (Dumbs down
I have not really yet seen a need for an abstract method in Ruby.
If you are looking for warnings or errors for subclasses not
implementing the abstract method you will be disappointed at Compile
Time [which arguabley does not even exist in 1.8]
If you are happy with the runtime exception (because you do nice code
covering tests James' solution is ideal, anyway you cannot do
better ( or worse for must of us;) in Ruby.
Welcome to the word of dynamic typing.
Yes indeed, welcome
James Edward Gray II
Robert
On 7/12/07, James Edward Gray II <james@grayproductions.net> wrote:
On Jul 11, 2007, at 5:40 PM, cypher.dp@gmail.com wrote:
--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck
It's true that an exception toss is the default behavior, but it's a
NoMethodError instead of a NotImplementedError and that could be an
important distinction, depending. And I agree with Robert Dober that
it could get you caught by method_missing one day.
What gets me about NoMethodError is you get that whether there's no
method at all (which follows from the name) or as a result of going
against access control (calling a private or protected method).
On Jul 11, 5:44 pm, James Edward Gray II <j...@grayproductions.net> wrote:
On Jul 11, 2007, at 5:40 PM, cypher...@gmail.com wrote:
> 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
endYou 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
--
-yossef
> 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
endYou may just want to skip this step altogether though, since an
exception toss is the default behavior.Hmm, I would say no, you will be caught by #method_missing one day.
It would be interesting why you want to do this?
If someone adds a method_missing() implementation to a child class that accepts messages it shouldn't, there is a bug in that implementation and it needs to be fixed. Your tests are going to catch that for you.
For documentation purpose?
That is about the best reason to add the stub, I think.
If you are looking for warnings or errors for subclasses not
implementing the abstract method you will be disappointed at Compile
Time [which arguabley does not even exist in 1.8]
class AbtractClass
REQUIRED_METHODS = %w[one two three]
def self.inherited(subclass)
REQUIRED_METHODS.each do |m|
raise "An implementation of #{m} is required by #{self.class}" \
unless subclass.instance_methods.include? m
end
end
end
class Broken < AbtractClass
def one; end
def two; end
end
# ~> -:7:in `inherited': An implementation of one is required by Class (RuntimeError)
# ~> from -:5:in `each'
# ~> from -:5:in `inherited'
# ~> from -:12
But don't do that.
James Edward Gray II
On Jul 12, 2007, at 7:05 AM, Robert Dober wrote:
On 7/12/07, James Edward Gray II <james@grayproductions.net> wrote:
On Jul 11, 2007, at 5:40 PM, cypher.dp@gmail.com wrote:
Hi,
Not sure if this has been mentioned already, but you might also be interested in checking out http://rubyforge.org/projects/abstract/
George
On 12 Jul 2007, at 14:32, James Edward Gray II wrote:
On Jul 12, 2007, at 7:05 AM, Robert Dober wrote:
On 7/12/07, James Edward Gray II <james@grayproductions.net> wrote:
On Jul 11, 2007, at 5:40 PM, cypher.dp@gmail.com wrote:
> 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
endYou may just want to skip this step altogether though, since an
exception toss is the default behavior.Hmm, I would say no, you will be caught by #method_missing one day.
It would be interesting why you want to do this?If someone adds a method_missing() implementation to a child class that accepts messages it shouldn't, there is a bug in that implementation and it needs to be fixed. Your tests are going to catch that for you.
For documentation purpose?
That is about the best reason to add the stub, I think.
If you are looking for warnings or errors for subclasses not
implementing the abstract method you will be disappointed at Compile
Time [which arguabley does not even exist in 1.8]class AbtractClass
REQUIRED_METHODS = %w[one two three]def self.inherited(subclass)
REQUIRED_METHODS.each do |m|
raise "An implementation of #{m} is required by #{self.class}" \
unless subclass.instance_methods.include? m
end
end
endclass Broken < AbtractClass
def one; end
def two; end
end
# ~> -:7:in `inherited': An implementation of one is required by Class (RuntimeError)
# ~> from -:5:in `each'
# ~> from -:5:in `inherited'
# ~> from -:12But don't do that.
James Edward Gray II
<snip>
> If you are looking for warnings or errors for subclasses not
> implementing the abstract method you will be disappointed at Compile
> Time [which arguabley does not even exist in 1.8]class AbtractClass
REQUIRED_METHODS = %w[one two three]def self.inherited(subclass)
REQUIRED_METHODS.each do |m|
raise "An implementation of #{m} is required by #{self.class}" \
unless subclass.instance_methods.include? m
end
end
endclass Broken < AbtractClass
def one; end
def two; end
end
# ~> -:7:in `inherited': An implementation of one is required by
Class (RuntimeError)
# ~> from -:5:in `each'
# ~> from -:5:in `inherited'
# ~> from -:12But don't do that.
That does not work as you have intended, it is complaining about #one
missing as a matter of fact:
531/31 > cat subclass.rb && ruby subclass.rb
# vim: sw=2 ts=2 ft=ruby
class P
def self.inherited(subclass)
puts "P --> #{subclass}"
end
end
class C < P
puts "in C"
end
P --> C
in C
Robert
On 7/12/07, James Edward Gray II <james@grayproductions.net> wrote:
--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck
You are right. My mistake.
James Edward Gray II
On Jul 12, 2007, at 9:17 AM, Robert Dober wrote:
On 7/12/07, James Edward Gray II <james@grayproductions.net> wrote:
<snip>> If you are looking for warnings or errors for subclasses not
> implementing the abstract method you will be disappointed at Compile
> Time [which arguabley does not even exist in 1.8]class AbtractClass
REQUIRED_METHODS = %w[one two three]def self.inherited(subclass)
REQUIRED_METHODS.each do |m|
raise "An implementation of #{m} is required by #{self.class}" \
unless subclass.instance_methods.include? m
end
end
endclass Broken < AbtractClass
def one; end
def two; end
end
# ~> -:7:in `inherited': An implementation of one is required by
Class (RuntimeError)
# ~> from -:5:in `each'
# ~> from -:5:in `inherited'
# ~> from -:12But don't do that.
That does not work as you have intended, it is complaining about #one
missing as a matter of fact: