From: Angel Martin Date: 2004-03-29T13:25:16+09:00 Subject: Re: How to pass a given block to a subroutine 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 :-( 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 :-) > 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