How do I change this to allow class A to get the block. Bascially I
want to override a method to do some pre-processing, but call the
superclass to actually implement the method. The superclass method
uses a block so how do I pass that block to the superclass?
To pass blocks around you have to use proc arguments.
If you declare a method thus:
def method2 (argument, &block)
end
Then any block associated with a call to the method will be turned into
a Proc object and assigned to the &-marked parameter (which must be last
in the list). Like *, this works both ways; if you pass a Proc as the
last parameter of a method call and put a & in front of it, the called
method sees it as a block.
So this should do what you want:
class B < A
def method1( argument, &block )
puts argument
super.method1( argument, &block )
end
end
-Mark
···
On Sun, Jul 27, 2003 at 07:58:50AM -0700, Eric Anderson wrote:
If I have
**** CODE ****
class A
def method1( argument )
yield( argument )
end
end
class B < A
def method1( argument )
puts argument
super.method1( argument )
end
end
class B < A
def method1( argument, &block )
puts argument
super( argument, &block )
end
end
Regards,
Brian.
···
On Wed, Jul 30, 2003 at 11:09:47AM +0900, Mark J. Reed wrote:
If you declare a method thus:
def method2 (argument, &block)
end
Then any block associated with a call to the method will be turned into
a Proc object and assigned to the &-marked parameter (which must be last
in the list). Like *, this works both ways; if you pass a Proc as the
last parameter of a method call and put a & in front of it, the called
method sees it as a block.
So this should do what you want:
class B < A
def method1( argument, &block )
puts argument
super.method1( argument, &block )
end
end