# Supplying multiple procs to initialization

**URL:** <https://www.ruby-forum.com/t/supplying-multiple-procs-to-initialization/121231>\
**Category:** Ruby\
**Created:** [November 25, 2007, 7:41pm UTC](https://www.ruby-forum.com/t/supplying-multiple-procs-to-initialization/121231 "2007-11-25T19:41:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [November 25, 2007, 7:41pm UTC](https://www.ruby-forum.com/t/supplying-multiple-procs-to-initialization/121231/1 "2007-11-25T19:41:13Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [November 25, 2007, 8:46pm UTC](https://www.ruby-forum.com/t/supplying-multiple-procs-to-initialization/121231/2 "2007-11-25T20:46:51Z")

</div>

On 25.11.2007 19:36, Phrogz wrote:

> }  
> owl.handle\_wiki\_command do |tag, command, params|  
> owl.handle\_wiki\_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

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [November 25, 2007, 9:06pm UTC](https://www.ruby-forum.com/t/supplying-multiple-procs-to-initialization/121231/3 "2007-11-25T21:06:14Z")

</div>

On Nov 25, 12:42 pm, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > # do something useful to the link
> > 
> > ```
> > 
> > owl = OWLScribble.new( markup ) do |owl|  
> > these two procs. Alternatives that I have rejected as less elegant:  
> > do\_command = proc{ … }
> 
> 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.

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [November 26, 2007, 8:40am UTC](https://www.ruby-forum.com/t/supplying-multiple-procs-to-initialization/121231/4 "2007-11-26T08:40:03Z")

</div>

2007/11/25, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > owl.wiki\_links.each{ |link|
> > 
> > > The syntax you see above is my current favorite choice for specifying  
> > > do\_link = proc{ … }  
> > > basis?  
> > > 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.

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
