Method type 'abstract' 
Author Message
 Method type 'abstract'

The one thing I miss in Ruby is the abstract class method to go along
with public and private. I know it is easy to work around by having the
default abstract class simply raise an exception but would it be
difficult to implement this as part of Ruby?


Sun, 29 Aug 2004 00:27:18 GMT  
 Method type 'abstract'
In Ruby *all* methods are abstract unless implemented by a class.

Or to put it in a more useful way, abstract methods are not required in a
dynamically typed language such as Ruby, only in typed languages like Java
or C++.

________________________________
Dr. Nathaniel Pryce
B13media Ltd.
40-41 Whiskin St, London, EC1R 0BP, UK
http://www.b13media.com

Quote:
----- Original Message -----


Sent: Tuesday, March 12, 2002 4:24 PM
Subject: Method type 'abstract'

> The one thing I miss in Ruby is the abstract class method to go along
> with public and private. I know it is easy to work around by having the
> default abstract class simply raise an exception but would it be
> difficult to implement this as part of Ruby?



Sun, 29 Aug 2004 00:34:54 GMT  
 Method type 'abstract'

Quote:

>In Ruby *all* methods are abstract unless implemented by a class.

>Or to put it in a more useful way, abstract methods are not required in a
>dynamically typed language such as Ruby, only in typed languages like Java
>or C++.

Not quite what I had in mind. If I create a class I will provide various
methods that a subclass can override. But in many cases they will pick
up the super classes' methods. However in the class I have in mind there
is one method that must exist in the subclass for which there is no
meaningfull default action that it can undertake, the subclass must
override this method. In Java I would mark the method as abstract rather
than just public or private and when I subclass it the compiler would
{*filter*}if I did not provide the override in the subclass.

At present I have to insert a normal method in the class that raises an
exception which will then cause the subclass to fall over if it did not
override it. It works but it isn't clean, to my mind abstract is just as
fundemental as public or private and therefore should be marked as such.

I have a deep suspicion that the way Ruby does things means that this is
not possible, just thought I would ask though.



Sun, 29 Aug 2004 00:59:57 GMT  
 Method type 'abstract'
..

Quote:
> override this method. In Java I would mark the method as abstract rather
> than just public or private and when I subclass it the compiler would
> {*filter*}if I did not provide the override in the subclass.

In Ruby, adding methods is dynamic, so the compiler can never be sure
that you aren't going to provide the override at some point in the
future. So the Ruby way to detect this is at "run time", i.e. with a
superclass method that raises an exception.


Sun, 29 Aug 2004 01:13:29 GMT  
 Method type 'abstract'
In a statically typed language, such as Java, the compiler would catch that
you had not implemented an abstract method.  In a dynamically typed language
like Ruby, one uses unit tests for this:

* Write tests that define the required behaviour of all classes derived from
your abstract class.
* Run those tests over an instance of each concrete class.

In practice, you have to use unit tests for this in statically typed
languages as well, because although a concrete class may implement an
abstract method, it may not implement it correctly.  There are many
potential errors that cannot be caught by the type system.

Cheers,
            Nat.
________________________________
Dr. Nathaniel Pryce
B13media Ltd.
40-41 Whiskin St, London, EC1R 0BP, UK
http://www.*-*-*.com/

Quote:
----- Original Message -----


Sent: Tuesday, March 12, 2002 4:57 PM
Subject: Re: Method type 'abstract'


> >In Ruby *all* methods are abstract unless implemented by a class.

> >Or to put it in a more useful way, abstract methods are not required in a
> >dynamically typed language such as Ruby, only in typed languages like
Java
> >or C++.

> Not quite what I had in mind. If I create a class I will provide various
> methods that a subclass can override. But in many cases they will pick
> up the super classes' methods. However in the class I have in mind there
> is one method that must exist in the subclass for which there is no
> meaningfull default action that it can undertake, the subclass must
> override this method. In Java I would mark the method as abstract rather
> than just public or private and when I subclass it the compiler would
> {*filter*}if I did not provide the override in the subclass.

> At present I have to insert a normal method in the class that raises an
> exception which will then cause the subclass to fall over if it did not
> override it. It works but it isn't clean, to my mind abstract is just as
> fundemental as public or private and therefore should be marked as such.

> I have a deep suspicion that the way Ruby does things means that this is
> not possible, just thought I would ask though.



Sun, 29 Aug 2004 01:16:20 GMT  
 Method type 'abstract'

Quote:
> At present I have to insert a normal method in the class that raises an
> exception which will then cause the subclass to fall over if it did not
> override it. It works but it isn't clean, to my mind abstract is just as
> fundemental as public or private and therefore should be marked as such.

Why not just *not* implement the method at all and let method_missing throw
the exception for you?

That's what I meant when I said that in Ruby all methods are abstract unless
explicitly implemented.

Cheers,
            Nat.



Sun, 29 Aug 2004 01:24:36 GMT  
 Method type 'abstract'

Quote:

>Why not just *not* implement the method at all and let method_missing throw
>the exception for you?

>That's what I meant when I said that in Ruby all methods are abstract unless
>explicitly implemented.

>Cheers,
>            Nat.

That looks interesting. I'll have to look at it.

Thanks.



Sun, 29 Aug 2004 01:29:24 GMT  
 Method type 'abstract'

Quote:


> >In Ruby *all* methods are abstract unless implemented by a class.

> >Or to put it in a more useful way, abstract methods are not required in a
> >dynamically typed language such as Ruby, only in typed languages like Java
> >or C++.

> Not quite what I had in mind. If I create a class I will provide various
> methods that a subclass can override. But in many cases they will pick
> up the super classes' methods. However in the class I have in mind there
> is one method that must exist in the subclass for which there is no
> meaningfull default action that it can undertake, the subclass must
> override this method. In Java I would mark the method as abstract rather
> than just public or private and when I subclass it the compiler would
> {*filter*}if I did not provide the override in the subclass.

In Smalltalk it is common to implement this method in the superclass and
have it call "subclassResponsibility", a method defined in Object.

You could throw an exception. That's very useful for unit testing.

Jim
--

"Alas! The onion you are eating is someone else's waterlily."
    -- A fortune cookie fortune



Sun, 29 Aug 2004 01:54:37 GMT  
 Method type 'abstract'

Quote:


> > At present I have to insert a normal method in the class that raises an
> > exception which will then cause the subclass to fall over if it did not
> > override it. It works but it isn't clean, to my mind abstract is just as
> > fundemental as public or private and therefore should be marked as such.

> Why not just *not* implement the method at all and let method_missing throw
> the exception for you?

> That's what I meant when I said that in Ruby all methods are abstract unless
> explicitly implemented.

> Cheers,
>             Nat.

Would you mind explaining it more friendly?
What you say seems to be interesting, but I cannot understand.
If you are a busy man and have no time to explain, please ignore
my request.  
# I hope you have enough time.

Regards,
kwatch



Sun, 29 Aug 2004 11:43:46 GMT  
 Method type 'abstract'

Quote:




>>> At present I have to insert a normal method in the class that raises an
>>> exception which will then cause the subclass to fall over if it did not
>>> override it. It works but it isn't clean, to my mind abstract is just as
>>> fundemental as public or private and therefore should be marked as such.

>>Why not just *not* implement the method at all and let method_missing throw
>>the exception for you?

>That would be a problem in classes where you define method_missing for
>some other use.

>I'm content with throwing an exception in methods that are 'abstract' (ie.
>methods that should be overridden by subclasses), but perhaps we can make
>that easier by defining an 'abstract' method within Module, like:

The code I posted previously doesn't work, however I played with it and
came up with the following code that does work:

class AbstractException < Exception
end

class Module
  def abstract(*methods)
    methods.each { |s|
      class_eval("def #{s}; raise AbstractException,'#{s} should be
overriden!'; end")
    }
  end
end

class MyClass
  abstract :some_method
end

mc = MyClass.new
mc.some_method
#end

Now if you run this you'll get:

(eval):1:in `some_method': some_method should be overriden!
(AbstractException) from abstract.rb:23

Now you can create a subclass of MyClass and override 'some_method', like:

class OtherClass < MyClass
  def some_method
    puts "OtherClass::some_method"
  end
end

oc = OtherClass.new
oc.some_method #=> "OtherClass::some_method"

Phil



Sun, 29 Aug 2004 03:11:55 GMT  
 Method type 'abstract'

Quote:


>> At present I have to insert a normal method in the class that raises an
>> exception which will then cause the subclass to fall over if it did not
>> override it. It works but it isn't clean, to my mind abstract is just as
>> fundemental as public or private and therefore should be marked as such.

>Why not just *not* implement the method at all and let method_missing throw
>the exception for you?

That would be a problem in classes where you define method_missing for
some other use.

I'm content with throwing an exception in methods that are 'abstract' (ie.
methods that should be overridden by subclasses), but perhaps we can make
that easier by defining an 'abstract' method within Module, like:

#NOTE: I have not tried this code, it probably doesn't work!!!
class AbstractException < Exception
end

class Module
  def abstract(*symbols)
    symbols.each { |s|
      instance_eval("def #{s}; raise AbstractException \"#{s} must be
overridden!\";end")
    }
  end
end

class MyClass

  abstract :some_method

end

mc = MyClass.new
mc.some_method #=> "some_method must be overridden!"

I'm not proposing this as an RCR, just as an idea that could be used to
acheive what the original poster wants to do.

Phil



Sun, 29 Aug 2004 02:35:13 GMT  
 Method type 'abstract'


Quote:
>The one thing I miss in Ruby is the abstract class method to go along
>with public and private. I know it is easy to work around by having the
>default abstract class simply raise an exception but would it be
>difficult to implement this as part of Ruby?

public, private and protected are methods defined in Module perhaps one
could define an abstract method in Module as well that would raise an
exception if a method defined as abstract is called on the class.

Phil



Sun, 29 Aug 2004 02:19:20 GMT  
 
 [ 18 post ]  Go to page: [1] [2]

 Relevant Pages 

1. 'Class'Input and abstract types

2. 'Access on current instance of abstract type

3. Deriving abstract types from abstract types

4. Hassan Ait-Kaci's _Warren's Abstract Machine_

5. f2003 - abstract types and abstract interfaces

6. vrml 2.0 activeX control that allows java methods to access container's methods

7. Recursive methods with :around's and call-next-method results

8. LabVIEW's equivalent to Excel's GoalSeek method

9. 'bind' functions into methods

10. Invoking a subclass's method on its superclass's instance

11. methods in 'C' - persistent data structures

12. 'capwords' is not a string method

 

 
Powered by phpBB® Forum Software