Supplying multiple procs to initialization

P

Phrogz

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?
 
R

Robert Klemme

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
 
P

Phrogz

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

Robert Klemme

2007/11/25 said:
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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

ANN main-4.4.0 0
[ANN] main-4.0.0 (for avdi) 0
[ANN] main-3.0.1 0
Net::SSH - request a pty seems to put stderr in stdout 2
[ANN] main-2.6.0 0
[ANN] main-2.8.3 2
[ANN] main-2.5.0 4
[ANN] main-2.2.0 2

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top