Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > method mixin?

Reply
Thread Tools

method mixin?

 
 
Cnm Cnm
Guest
Posts: n/a
 
      08-20-2010
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?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      08-20-2010
On 20.08.2010 03:03, Cnm Cnm wrote:
> 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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
method def in method vs method def in block Kyung won Cheon Ruby 0 11-21-2008 08:48 AM
invoke a method by reflection£¨the method's parameters can not be ArrayList? jerry051 ASP .Net 2 08-02-2005 10:35 AM
BC30289: Statement cannot appear within a method body. End of method assumed. Carlos Oliveira ASP .Net 0 08-19-2004 07:51 PM
Difference between Delete method and RemoveRow method CW ASP .Net 0 04-01-2004 01:07 AM
ASP.NET: BC30289: Statement cannot appear within a method body. End of method assumed. Mike Wilmot ASP .Net 0 12-15-2003 07:49 PM



Advertisments