Object#send and blocks/procs

Discussion in 'Ruby' started by Mehr, Assaph (Assaph), Sep 27, 2004.

  1. Hi Guys,

    I started to write this email as a question, but figured the answer in
    the middle... :)
    So now I want to know if there's any problem with the code below, if
    there is any performance hit or if there's a simpler way to turn Procs
    back into blocks.

    The question is how do I #send a method to an object with an attached
    proc (not block)? The simplest way to reproduce is:

    prc = lambda{ |a| a.upcase }
    puts "aaa".send:)gsub, /./) # how do I pass prc as the block to
    gsub?

    Solution:

    puts "aaa".send:)gsub, /./) { |x| prc[x] }

    i.e. I have wrapped my Proc object within a block. Some interspersing of
    '*' may be required for multiple params, as Markus pointed out in
    another thread.


    Cheers,
    Assaph
     
    Mehr, Assaph (Assaph), Sep 27, 2004
    #1
    1. Advertising

  2. Assaph wrote:

    > prc = lambda{ |a| a.upcase }
    > puts "aaa".send:)gsub, /./) # how do I pass prc as the block to gsub?


    Try this:

    puts "aaa".send:)gsub, /./, &prc)

    Cheers,
    Gavin
     
    Gavin Sinclair, Sep 27, 2004
    #2
    1. Advertising

  3. Mehr, Assaph (Assaph)

    George Ogata Guest

    "Mehr, Assaph (Assaph)" <> writes:

    >
    > Hi Guys,
    >
    > I started to write this email as a question, but figured the answer in
    > the middle... :)
    > So now I want to know if there's any problem with the code below, if
    > there is any performance hit or if there's a simpler way to turn Procs
    > back into blocks.
    >
    > The question is how do I #send a method to an object with an attached
    > proc (not block)? The simplest way to reproduce is:
    >
    > prc = lambda{ |a| a.upcase }
    > puts "aaa".send:)gsub, /./) # how do I pass prc as the block to
    > gsub?
    >
    > Solution:
    >
    > puts "aaa".send:)gsub, /./) { |x| prc[x] }


    puts 'aaa'.send:)gsub, /./, &prc)
     
    George Ogata, Sep 27, 2004
    #3
    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. Arjen
    Replies:
    3
    Views:
    461
    Scott Allen
    Feb 27, 2005
  2. matt
    Replies:
    1
    Views:
    287
    George Ogata
    Aug 6, 2004
  3. Eli Bendersky
    Replies:
    2
    Views:
    125
    Eli Bendersky
    Apr 16, 2006
  4. Eli Bendersky
    Replies:
    0
    Views:
    128
    Eli Bendersky
    Apr 18, 2006
  5. Steven Taylor
    Replies:
    9
    Views:
    277
    Brian Candler
    Apr 27, 2009
Loading...

Share This Page