Snippet to append code to method

R

Ray K.

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
 
R

Robert Klemme

2010/7/23 Ray K. said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top