How to pass a given block to a subroutine

Hello ,

I have a method ‘foo’ that takes a block, but the yield should
be done in a subroutine.

···

======================================================
class Example

    def foo (*args)
        subroutine *args
    end
    
    def subroutine *arg
        yield *arg # want to call the block given to foo
    end

end

a = Example.new
a.foo(10,20) { |a,b| print “#{a + b}\n” }

If you ask why i want to do this:
i have to replace an existing subroutine with my own version, that
does something before yield is called. So i renamed the method
and must now find a way to call it from my version.


Best regards,
Lothar mailto:mailinglists@scriptolutions.com

        def foo (*args)

           def foo(*args, &block)

            subroutine *args
  
               subroutine(*args, &block)

        end
        
Guy Decoux

Is this what you want?

/tmp % ruby -w y.rb
hello
world
/tmp % cat y.rb
def foo (*args, &ablock)
subroutine(*args, &ablock)
end

def subroutine *args
yield(*args)
end

foo(“hello”, “world”) { |a,b| puts a; puts b; }

Cheers,
Sam

Wrote Lothar Scholz mailinglists@scriptolutions.com, on Tue, Mar 23, 2004 at 11:32:04PM +0900:

···

Hello ,

I have a method ‘foo’ that takes a block, but the yield should
be done in a subroutine.

======================================================
class Example

    def foo (*args)
        subroutine *args
    end
    
    def subroutine *arg
        yield *arg # want to call the block given to foo
    end

end

a = Example.new
a.foo(10,20) { |a,b| print “#{a + b}\n” }

If you ask why i want to do this:
i have to replace an existing subroutine with my own version, that
does something before yield is called. So i renamed the method
and must now find a way to call it from my version.


Best regards,
Lothar mailto:mailinglists@scriptolutions.com


Sam Roberts sroberts@certicom.com

I recently implement that in this way:
class foo
def initialize
@pr=
end
def first(*args,&pr)
@pr.push(pr)
second(*args)
@pr.pop
end
def second(*args)
yield(*args)
end
def yield(*args) #this will be in other class,u overload Kernel, etc
@pr.last.call(*args)
end
end
This hide the path of calls to the destination function,encapsulate it
Also make sense of recursive blocks in the path
Regards,
Angel

···

El mar, 23-03-2004 a las 15:32, Lothar Scholz escribió:

Hello ,

I have a method ‘foo’ that takes a block, but the yield should
be done in a subroutine.

======================================================
class Example

    def foo (*args)
        subroutine *args
    end
    
    def subroutine *arg
        yield *arg # want to call the block given to foo
    end

end

a = Example.new
a.foo(10,20) { |a,b| print “#{a + b}\n” }

If you ask why i want to do this:
i have to replace an existing subroutine with my own version, that
does something before yield is called. So i renamed the method
and must now find a way to call it from my version.

    def foo (*args)
       def foo(*args, &block)
        subroutine *args
           subroutine(*args, &block)
    end

Guy Decoux

Im too slow… Decoux you are too fast… :wink:

ruby a.rb
30
expand -t2 a.rb
class Example
def foo(*args, &block)
subroutine(*args, &block)
end
def subroutine(*arg, &block)
block.call(*arg) # want to call the block given to foo
end
end
a = Example.new
a.foo(10,20) { |a,b| print “#{a + b}\n” }

···

On Tue, 23 Mar 2004 23:37:14 +0900, ts wrote:


Simon Strandgaard

s/yield/block/g
Sorry for not check it.
Apologize for the error, i think yield be in object or kernel.
In my code i called “block” when i type “yield”, once i change it i
found that yield not respond to overload it :frowning:

It’s a way to overload it or it has a callback associated?

IMHO i think this help to encapsulate the complexity of a given set of
functions to the caller.
The caller only have to call yield without care of what work is done
behind.
Currently i’am trying(i’m newbie to ruby) to create some kind of plugin
system.
My idea is to make the creation of plugins easy, or at least no very
complex.
The plugin manager search for .rb files in a directory, load it, and
wait for the client request a service.
Manager check if the service is avalaible, and call it if is possible.
For this is the needed storage of procs in the plugin manager(for this
and for:
plug.request(“repaint_wait”) {
plug.request(“all_components”){ |p|
p.repaint
}
}
)
In this scenario will be good to the plugin writer not care the name of
the special method i wrote to subtitute “yield” method.
With the overload yield the code will be (IMHO) more flexible supporting
several steps between “request” and final function in a tranparent way.
Anyway “block” it’s no so bad :slight_smile:

class Example

    def foo (*args)
        subroutine *args
    end
    
    def subroutine *arg
        yield *arg # want to call the block given to foo
    end

end

a = Example.new
a.foo(10,20) { |a,b| print “#{a + b}\n” }

If you ask why i want to do this:
i have to replace an existing subroutine with my own version, that
does something before yield is called. So i renamed the method
and must now find a way to call it from my version.
I recently implement that in this way:
class foo
def initialize
@pr=
end
def first(*args,&pr)
@pr.push(pr)
second(*args)
@pr.pop
end
def second(*args)
yield(*args)
end
def yield(*args) #this will be in other class,u overload Kernel, etc
@pr.last.call(*args)
end
end

Best regards,
Angel