Snippet to append code to method

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

2010/7/23 Ray K. [email protected]:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs