Hijack, or alias a method with a proc?

Discussion in 'Ruby' started by Jim, May 18, 2007.

  1. Jim

    Jim Guest

    Hello,

    I have a Menu class with a add_item method, which takes a name
    followed by a proc:

    Menu("TopLevelMenuName").add_item("NewMenuItemName") { puts "Hello" }

    I want to hijack this method so I can customize the menu in which the
    proc is associated.

    Class Menu
    alias :_add_item :add_item
    def add_item( *args )
    p args
    end
    end

    This captures the menuname string, but how do I get at the associated
    proc?

    Thanks.
     
    Jim, May 18, 2007
    #1
    1. Advertisements

  2. Jim wrote:
    > Hello,
    >
    > I have a Menu class with a add_item method, which takes a name
    > followed by a proc:
    >
    > Menu("TopLevelMenuName").add_item("NewMenuItemName") { puts "Hello" }
    >
    > I want to hijack this method so I can customize the menu in which the
    > proc is associated.
    >
    > Class Menu
    > alias :_add_item :add_item
    > def add_item( *args )


    def add_item( *args , &block )

    > p args

    p args, block
    _add_item(*args, &block)

    > end
    > end
    >
    > This captures the menuname string, but how do I get at the associated
    > proc?
    >
    > Thanks.
    >



    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, May 18, 2007
    #2
    1. Advertisements

  3. Jim

    Jim Guest

    Thanks Joel, I was certain it could be done. I appreciate your time
    and knowledge.
    On May 18, 2:14 pm, Joel VanderWerf <> wrote:
     
    Jim, May 20, 2007
    #3
    1. Advertisements

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. David Lozzi
    Replies:
    3
    Views:
    2,251
    David Lozzi
    Jun 1, 2005
  2. Replies:
    1
    Views:
    2,533
    nautonnier
    Jul 11, 2006
  3. NevilleDNZ
    Replies:
    9
    Views:
    650
    NevilleDNZ
    Aug 16, 2006
  4. grocery_stocker
    Replies:
    9
    Views:
    1,064
    grocery_stocker
    May 24, 2008
  5. richard
    Replies:
    8
    Views:
    637
    Adrienne Boswell
    Nov 21, 2008
  6. Dave

    Hijack Session

    Dave, Jan 8, 2004, in forum: ASP .Net Security
    Replies:
    2
    Views:
    205
    Keith
    Feb 1, 2004
  7. Jean-Hugues ROBERT

    Why no Proc##[]=() ? Why no Proc##replace() ?

    Jean-Hugues ROBERT, May 1, 2004, in forum: Ruby
    Replies:
    14
    Views:
    504
    Jean-Hugues ROBERT
    May 5, 2004
  8. Minkoo Seo

    Proc vs lambda vs proc

    Minkoo Seo, Feb 4, 2007, in forum: Ruby
    Replies:
    19
    Views:
    487
    Brian Candler
    Feb 6, 2007
Loading...