[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Abstract methods in ruby ?

cypher.dp

7/11/2007 10:41:00 PM

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

11 Answers

James Gray

7/11/2007 10:45:00 PM

0

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

cypher.dp

7/12/2007 12:44:00 AM

0

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

Robert Dober

7/12/2007 12:05:00 PM

0

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
> end
>
> You 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

--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck

Yossef Mendelssohn

7/12/2007 12:48:00 PM

0


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

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

--
-yossef


James Gray

7/12/2007 1:33:00 PM

0

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
>> end
>>
>> You 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

George Malamidis

7/12/2007 1:53:00 PM

0

Hi,

Not sure if this has been mentioned already, but you might also be
interested in checking out http://rubyforge.org/projects...

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
>>> end
>>>
>>> You 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
>


Robert Dober

7/12/2007 2:18:00 PM

0

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
> 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. ;)
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

--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck

James Gray

7/12/2007 2:45:00 PM

0

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
>> 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. ;)
> That does not work as you have intended, it is complaining about #one
> missing as a matter of fact:

You are right. My mistake.

James Edward Gray II

Thomas Hafner

7/21/2007 10:56:00 AM

0

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

Gary DW

12/13/2010 6:50:00 AM

0

In article <68219b70-7f01-4ec0-b121-
24beef0ef0fe@n32g2000pre.googlegroups.com>, wryan77@gmail.com,WR says...


>
> On Dec 6, 8:07?pm, Phlip <phlip2...@gmail.com> wrote:
> > On Dec 6, 4:52?pm, Geo <geotax...@yahoo.com> wrote:
> >
> > > Wait a second, I thought there were no regulations!
> >
> > You mean "Bush deregulation caused the bubble"?
> >
> > No regulations would be bad. Fair deregulation, to simplify things and
> > level the playing field, would be good.
> >
> > We didn't get either. We got custom loopholes, sold to the highest
> > bidders to exploit.
>
> It was nastier than that. The banks engaged in massive fraud. They
> bundled worthless mortgages and then rated the bundled securities
> triple A, foised them on the market, and, when they began to sense the
> tumble, sold short on their own offerings.

Limbaugh and the wanna-be Limbaughs never mention this, so there isn't a
rightie in the entire country that knows this factual information.



The rich got one hell of
> lot richer, and the rest of the country was stuck holding the bag.


If we bailed AIG out with the stipulation that any of their CDS
repayments only could be made if the companies accepting them would
forgo all corporate bonuses for the next 20 years, the problem would
have been solved.

Goldman Sachs would have had to choose between bankruptsy, or an intact
business with their CEOs operating without bonuses for a long long time.
Of course they would have chosen the latter.

Instead they are now enjoying massive profits and the CEO's are banking
billions of bonuses again.

What lesson was learned?

Crime DOES pay?

The
> rich are still doing damn well. Last year, the 25 top hedge fund
> managers earned over $1 billion each. Guess who eventually pays for
> that.


Ding ding ding...


--
And I'll never forgive the Bush administration and Paulson for basically
destroying the last vestige of fiscal responsibility that we had in the
Republican Party. After that, I don't know how we ever make the tough
choices..

David Stockman, Reagan budget director