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