# Supplying multiple procs to initialization

**URL:** <https://rubytalk.org/t/supplying-multiple-procs-to-initialization/42492>\
**Category:** ruby-talk\
**Created:** [25 November 2007 18:40 UTC](https://rubytalk.org/t/supplying-multiple-procs-to-initialization/42492 "2007-11-25T18:40:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [25 November 2007 18:40 UTC](https://rubytalk.org/t/supplying-multiple-procs-to-initialization/42492/1 "2007-11-25T18:40:03Z")

</div>

One of my libraries previously required code like this:

&nbsp;&nbsp;# Convert markup to HTML, doing something  
&nbsp;&nbsp;# useless with links and commands  
&nbsp;&nbsp;owl = OWLScribble.new( markup )  
&nbsp;&nbsp;owl.wiki\_links.each{ |link|  
&nbsp;&nbsp;&nbsp;&nbsp;# do something useful to the link  
&nbsp;&nbsp;}  
&nbsp;&nbsp;owl.wiki\_commands.each{ |command|  
&nbsp;&nbsp;&nbsp;&nbsp;# do something useful with the command  
&nbsp;&nbsp;}  
&nbsp;&nbsp;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:

&nbsp;&nbsp;owl = OWLScribble.new( markup ) do |owl|  
&nbsp;&nbsp;&nbsp;&nbsp;owl.handle\_wiki\_link do |tag, page, link\_text|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do something useful to the link  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;owl.handle\_wiki\_command do |tag, command, params|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do something useful to the command  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;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:

&nbsp;&nbsp;# Deferred initialization  
&nbsp;&nbsp;owl = OWLScribble.new( markup )  
&nbsp;&nbsp;owl.handle\_wiki\_link{ ... }  
&nbsp;&nbsp;owl.handle\_wiki\_command{ ... }  
&nbsp;&nbsp;owl.run  
&nbsp;&nbsp;puts owl.to\_html

&nbsp;&nbsp;# proc params  
&nbsp;&nbsp;do\_link = proc{ ... }  
&nbsp;&nbsp;do\_command = proc{ ... }  
&nbsp;&nbsp;owl = OWLScribble.new(  
&nbsp;&nbsp;&nbsp;&nbsp;markup,  
&nbsp;&nbsp;&nbsp;&nbsp;:handle\_wiki\_link=\>do\_link,  
&nbsp;&nbsp;&nbsp;&nbsp;:handle\_wiki\_command=\>do\_command  
&nbsp;&nbsp;)  
&nbsp;&nbsp;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?

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [25 November 2007 19:45 UTC](https://rubytalk.org/t/supplying-multiple-procs-to-initialization/42492/2 "2007-11-25T19:45:08Z")

</div>

Why not just

&nbsp;&nbsp;&nbsp;owl = OWLScribble.new(  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;markup,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:handle\_wiki\_link =\> lambda {...},  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:handle\_wiki\_command =\> lambda {...}  
&nbsp;&nbsp;&nbsp;)

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 25.11.2007 19:36, Phrogz wrote:
> 
> > One of my libraries previously required code like this:
> > 
> > &nbsp;&nbsp;# Convert markup to HTML, doing something  
> > &nbsp;&nbsp;# useless with links and commands  
> > &nbsp;&nbsp;owl = OWLScribble.new( markup )  
> > &nbsp;&nbsp;owl.wiki\_links.each{ |link|  
> > &nbsp;&nbsp;&nbsp;&nbsp;# do something useful to the link  
> > &nbsp;&nbsp;}  
> > &nbsp;&nbsp;owl.wiki\_commands.each{ |command|  
> > &nbsp;&nbsp;&nbsp;&nbsp;# do something useful with the command  
> > &nbsp;&nbsp;}  
> > &nbsp;&nbsp;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:
> > 
> > &nbsp;&nbsp;owl = OWLScribble.new( markup ) do |owl|  
> > &nbsp;&nbsp;&nbsp;&nbsp;owl.handle\_wiki\_link do |tag, page, link\_text|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do something useful to the link  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;owl.handle\_wiki\_command do |tag, command, params|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do something useful to the command  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;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:
> > 
> > &nbsp;&nbsp;# Deferred initialization  
> > &nbsp;&nbsp;owl = OWLScribble.new( markup )  
> > &nbsp;&nbsp;owl.handle\_wiki\_link{ ... }  
> > &nbsp;&nbsp;owl.handle\_wiki\_command{ ... }  
> > &nbsp;&nbsp;owl.run  
> > &nbsp;&nbsp;puts owl.to\_html
> > 
> > &nbsp;&nbsp;# proc params  
> > &nbsp;&nbsp;do\_link = proc{ ... }  
> > &nbsp;&nbsp;do\_command = proc{ ... }  
> > &nbsp;&nbsp;owl = OWLScribble.new(  
> > &nbsp;&nbsp;&nbsp;&nbsp;markup,  
> > &nbsp;&nbsp;&nbsp;&nbsp;:handle\_wiki\_link=\>do\_link,  
> > &nbsp;&nbsp;&nbsp;&nbsp;:handle\_wiki\_command=\>do\_command  
> > &nbsp;&nbsp;)  
> > &nbsp;&nbsp;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?

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [25 November 2007 20:05 UTC](https://rubytalk.org/t/supplying-multiple-procs-to-initialization/42492/3 "2007-11-25T20:05:10Z")

</div>

I personally lump that in with the other option that happens to create  
the procs outside the function call. As you show it, with a single  
ellipsis for each proc's content, it feels clean. I expect  
handle\_wiki\_link to be on the order of 20 lines, and  
handle\_wiki\_command to take 50-100 lines. That seems excessive to  
inline into a method call.

> **···**
>
> On Nov 25, 12:42 pm, Robert Klemme \<shortcut...@googlemail.com\> wrote:
> 
> > 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
> > 
> > &nbsp;&nbsp;&nbsp;owl = OWLScribble.new(  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;markup,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:handle\_wiki\_link =\> lambda {...},  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:handle\_wiki\_command =\> lambda {...}  
> > &nbsp;&nbsp;&nbsp;)

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [26 November 2007 07:39 UTC](https://rubytalk.org/t/supplying-multiple-procs-to-initialization/42492/4 "2007-11-26T07:39:12Z")

</div>

Good point. I did not know that they would become that complex. In  
that case it's a reasonable option to define them elsewhere. But in  
that case I'd probably even define them as constants - unless you need  
the local binding.

Kind regards

robert

> **···**
>
> 2007/11/25, Phrogz \<phrogz@mac.com\>:
> 
> > On Nov 25, 12:42 pm, Robert Klemme \<shortcut...@googlemail.com\> wrote:  
> > \> 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 {...}  
> > \> )
> > 
> > I personally lump that in with the other option that happens to create  
> > the procs outside the function call. As you show it, with a single  
> > ellipsis for each proc's content, it feels clean. I expect  
> > handle\_wiki\_link to be on the order of 20 lines, and  
> > handle\_wiki\_command to take 50-100 lines. That seems excessive to  
> > inline into a method call.
> 
> --  
> use.inject do |as, often| as.you\_can - without end
