Snippet to append code to method

Discussion in 'Ruby' started by Ray K., Jul 23, 2010.

  1. Ray K.

    Ray K. Guest

    Hello from Germany,

    I'm looking for a brief snippet that I've seen somewhere lately, but
    can't remember where.

    It did override Object so one could add code to an existing method -
    without just overriding it.

    Anyone have a clou? I'll keep looking, maybe I can provide more details
    later.

    Thanks
    Ray
    --
    Posted via http://www.ruby-forum.com/.
     
    Ray K., Jul 23, 2010
    #1
    1. Advertising

  2. 2010/7/23 Ray K. <>:
    > Hello from Germany,


    Hi Timezonefellow,

    > I'm looking for a brief snippet that I've seen somewhere lately, but
    > can't remember where.
    >
    > It did override Object so one could add code to an existing method -
    > without just overriding it.
    >
    > Anyone have a clou? I'll keep looking, maybe I can provide more details
    > later.


    Rainer, vielleicht meinst Du so etwas?

    irb(main):001:0> class Foo
    irb(main):002:1> def bar
    irb(main):003:2> puts "bar"
    irb(main):004:2> end
    irb(main):005:1> end
    => nil
    irb(main):006:0> f = Foo.new
    => #<Foo:0x1015ce1c>
    irb(main):007:0> f.bar
    bar
    => nil
    irb(main):008:0> class Foo
    irb(main):009:1> alias _bar bar
    irb(main):010:1> def bar
    irb(main):011:2> puts "new bar before"
    irb(main):012:2> _bar
    irb(main):013:2> puts "new bar after"
    irb(main):014:2> end
    irb(main):015:1> end
    => nil
    irb(main):016:0> f.bar
    new bar before
    bar
    new bar after
    => nil
    irb(main):017:0>

    Cheers

    robert


    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Jul 23, 2010
    #2
    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. Clare Hsiao
    Replies:
    0
    Views:
    348
    Clare Hsiao
    Feb 12, 2004
  2. Tom
    Replies:
    3
    Views:
    432
    Jeffrey Palermo [MCP]
    Dec 1, 2004
  3. Edwin Knoppert
    Replies:
    0
    Views:
    380
    Edwin Knoppert
    Nov 30, 2005
  4. Gil Blais
    Replies:
    0
    Views:
    389
    Gil Blais
    Apr 15, 2004
  5. HYRY
    Replies:
    10
    Views:
    632
    Bruno Desthuilliers
    Sep 26, 2007
Loading...

Share This Page