[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Ruby black magic? Meta Programming

Tom Willis

3/12/2005 7:11:00 PM

My first exposure to Ruby was from Jim, and his "10 things every java
programmer should know about ruby" presentation.

Anyway, I probably shouldn't even be thinking about this kind of stuff
yet, but one thing in the presentation that I dug a lot was the use of
overriding Kernel#method_missing to queue messages and play them back
in order.

Anyway, I want to know what other kind of cool,i guess I'll call it
metaprogramming, can be done in ruby.


For example, I remember reading something about Eiffel having the
ability to declare pre and post conditions for method calls on
objects, essentially bringing this criteria in as part of the
interface. Can something like this be done in Ruby? I'm assuming so,
but for some reason my google hits keep referring me to Pyhon's
Metaclass things, go figure.

Anyone got a link?

TIA
--
Thomas G. Willis
http://paperbac...


4 Answers

ES

3/12/2005 7:32:00 PM

0

On Sat, March 12, 2005 7:11 pm, Tom Willis said:
> My first exposure to Ruby was from Jim, and his "10 things every java
> programmer should know about ruby" presentation.
>
> Anyway, I probably shouldn't even be thinking about this kind of stuff
> yet, but one thing in the presentation that I dug a lot was the use of
> overriding Kernel#method_missing to queue messages and play them back
> in order.
>
> Anyway, I want to know what other kind of cool,i guess I'll call it
> metaprogramming, can be done in ruby.
>
>
> For example, I remember reading something about Eiffel having the
> ability to declare pre and post conditions for method calls on
> objects, essentially bringing this criteria in as part of the
> interface. Can something like this be done in Ruby? I'm assuming so,
> but for some reason my google hits keep referring me to Pyhon's
> Metaclass things, go figure.
>
> Anyone got a link?

Currently pre/postconditions are proposed for Ruby 2.0, seehttp://www.rubygarden.org...

You can achieve the same effect by creating a meta for something like
def_pre :methodname, def_post :methodname with associated blocks. Then
alias the actual :methodname, make the pre block respond to :methodname;
pre executes its block and then calls :methodname, after which post block
is called.

> Thomas G. Willis

E



William Morgan

3/12/2005 7:55:00 PM

0

Excerpts from ES's mail of 12 Mar 2005 (EST):
> You can achieve the same effect by creating a meta for something like
> def_pre :methodname, def_post :methodname with associated blocks. Then
> alias the actual :methodname, make the pre block respond to
> :methodname; pre executes its block and then calls :methodname, after
> which post block is called.

See http://www.thekode.net/ruby/techniques/CapturingMe... for an
example implementation (which doesn't use alias).

But there are limitations to this technique that will only be resolved
in 2.0.

--
William <wmorgan-ruby-talk@masanjin.net>


Dave Burt

3/12/2005 8:38:00 PM

0

Bruce Willis <tom.willis@gmail.com> asked:
> My first exposure to Ruby was from Jim, and his "10 things every java
> programmer should know about ruby" presentation.
>
> Anyway, I probably shouldn't even be thinking about this kind of stuff
> yet, but one thing in the presentation that I dug a lot was the use of
> overriding Kernel#method_missing to queue messages and play them back
> in order.
>
> Anyway, I want to know what other kind of cool,i guess I'll call it
> metaprogramming, can be done in ruby.

I found this fun:
http://www.dave.burt.id.au/ruby/roman_n...

Cheers,
Dave


Mathieu Bouchard

3/13/2005 9:15:00 PM

0