method mixin?

C

Cnm Cnm

Just wonder is there any way to inject a bunch of code into an instance
method, just like how a module can be used as a mixin to a class?
 
R

Robert Klemme

Just wonder is there any way to inject a bunch of code into an instance
method, just like how a module can be used as a mixin to a class?

One thing you can do is alias the original and use it.

class Foo
def bar; puts 123; end
end

f = Foo.new
f.bar

class Foo
alias _bar bar

def bar
puts "before"
x = _bar
puts "after"
x
end
end

f.bar

I once did some meta programming to nicely declare before and after code
but that code was never production ready. IIRC others did similar things.

Kind regards

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
474,262
Messages
2,571,044
Members
48,769
Latest member
Clifft

Latest Threads

Top