[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how to "decorate" a method?

Matteo Gottardi

7/6/2007 9:11:00 AM

Hi all. How can I "decorate" a method? In python I do:

---- begin code ----
def decorato(func):
def decor(*args):
print "Before"
func(*args)
print "After"
return decor

@decorato
def metodo():
print "metodo"

metodo()
---- end code ----

and it will print:
Before
metodo
After

How can I make the same in ruby? I tried to look functions like "public"
or "private", but they are implemented in C, not in ruby...
PS: I'm not looking at a "decorate" syntax, only at a method that take a
method and returns a modified one...
For example:

def metodo
puts "metodo"
end

decorate :metodo

How can I implement the "decorate" function?

Thanks in advance, and please excuse my bad english...
--
* Matteo Gottardi | matgott@tin.it
* ICQ UIN 20381372
* Linux - the choice of a GNU generation
* GPG Fingerprint:
* B9EE 108F 52C8 D50C B667 B1F2 AB56 8A01 BA3D 36A1
1 Answer

gabriele renzi

7/6/2007 12:28:00 PM

0

On Fri, 06 Jul 2007 11:10:43 +0200, Matteo Gottardi wrote:


> How can I make the same in ruby? I tried to look functions like "public"
> or "private", but they are implemented in C, not in ruby...

You may find this[1] interesting. Basically explains a technique for
redefining methods that you can use for the same problems that python's
decorator often solve, even if it is not the same.

[1] http://www.thekode.net/ruby/techniques/CapturingMe...



--
goto 10: http://www...
blog it: http://riffraff.bl...
blog en: http://www.rif...