Supplying multiple procs to initialization

Discussion in 'Ruby' started by Phrogz, Nov 25, 2007.

  1. Phrogz

    Phrogz Guest

    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?
    Phrogz, Nov 25, 2007
    #1
    1. Advertising

  2. 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
    Robert Klemme, Nov 25, 2007
    #2
    1. Advertising

  3. Phrogz

    Phrogz Guest

    On Nov 25, 12:42 pm, Robert Klemme <> 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.
    Phrogz, Nov 25, 2007
    #3
  4. 2007/11/25, Phrogz <>:
    > On Nov 25, 12:42 pm, Robert Klemme <> 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.


    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

    --
    use.inject do |as, often| as.you_can - without end
    Robert Klemme, Nov 26, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QUtKVFI=?=

    Multiple stored procs for dataadapter

    =?Utf-8?B?QUtKVFI=?=, Dec 21, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    367
    Karl Seguin
    Dec 21, 2004
  2. =?Utf-8?B?U3llZCBHaGF5YXM=?=

    Problem writing cookie when supplying Path property

    =?Utf-8?B?U3llZCBHaGF5YXM=?=, May 6, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    432
    =?Utf-8?B?bWljcm9zb2Z0LnB1YmxpYy5kb3RuZXQuZnJhbWV3
    May 6, 2005
  3. sp
    Replies:
    3
    Views:
    690
    Philippe Poulard
    Jan 27, 2006
  4. Daniel Nichols
    Replies:
    6
    Views:
    416
    CBFalconer
    Jun 15, 2004
  5. matt
    Replies:
    1
    Views:
    240
    George Ogata
    Aug 6, 2004
Loading...

Share This Page