From: Robert Klemme Date: 2007-11-26T04:45:08+09:00 Subject: Re: Supplying multiple procs to initialization On 25.11.2007 19:36, Phrogz wrote: > One of my libraries previously required code like this: > > # Convert markup to HTML, doing something > # useless with links and commands > owl = OWLScribble.new( markup ) > owl.wiki_links.each{ |link| > # do something useful to the link > } > owl.wiki_commands.each{ |command| > # do something useful with the command > } > html = owl.to_html > > I have changed it so that the user can instead describe how to handle > links and commands in general, so that during initialization the > library doesn't waste time or code lines doing something useless: > > owl = OWLScribble.new( markup ) do |owl| > owl.handle_wiki_link do |tag, page, link_text| > # do something useful to the link > end > owl.handle_wiki_command do |tag, command, params| > # do something useful to the command > end > end > puts owl.to_html > > The syntax you see above is my current favorite choice for specifying > these two procs. Alternatives that I have rejected as less elegant: > > # Deferred initialization > owl = OWLScribble.new( markup ) > owl.handle_wiki_link{ ... } > owl.handle_wiki_command{ ... } > owl.run > puts owl.to_html > > # proc params > do_link = proc{ ... } > do_command = proc{ ... } > owl = OWLScribble.new( > markup, > :handle_wiki_link=>do_link, > :handle_wiki_command=>do_command > ) > puts owl.to_html > > Can anyone suggest a more elegant way of supplying multiple processing > procs that should be used during initialization, on a per-instance > basis? Why not just owl = OWLScribble.new( markup, :handle_wiki_link => lambda {...}, :handle_wiki_command => lambda {...} ) Kind regards robert