[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

method chaining via symbols

snacktime

9/5/2008 9:02:00 AM

If this is too strange just say so. Tonight I was working with a
rails helper method which accepts several symbols as arguments which
get turned into method calls. Normally it's used on activerecord
objects, but I wanted to make it work on an array of hashes instead.
That particular approach wouldn't work without being able to call
values.first, since some of the hash values were arrays themselves,
but the rails helper expected a single array.

So anyways I was thinking how it would work if you could do something like this:

send(:method1:method2:method3)

Arguments would be passed like normal, and the argument to a chained
method would be the result of the last method call.

Not sure I even like the idea, just curious if it's ever been discussed before?

Chris

2 Answers

James Coglan

9/5/2008 9:13:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

2008/9/5 snacktime <snacktime@gmail.com>

> If this is too strange just say so. Tonight I was working with a
> rails helper method which accepts several symbols as arguments which
> get turned into method calls. Normally it's used on activerecord
> objects, but I wanted to make it work on an array of hashes instead.
> That particular approach wouldn't work without being able to call
> values.first, since some of the hash values were arrays themselves,
> but the rails helper expected a single array.
>
> So anyways I was thinking how it would work if you could do something like
> this:
>
> send(:method1:method2:method3)
>
> Arguments would be passed like normal, and the argument to a chained
> method would be the result of the last method call.
>
> Not sure I even like the idea, just curious if it's ever been discussed
> before?
>
> Chris



Not sure if these are exactly what you're talking about, but these might
provide some inspiration:

http://jicksta.com/posts/the-metho...
http://blog.jayfields.com/2008/09/ruby-recording-method-call...

Both techniques allow you store store chains of method calls and replay them
later on some object.

James Coglan

9/5/2008 9:16:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

>
> So anyways I was thinking how it would work if you could do something like
> this:
>
> send(:method1:method2:method3)
>
> Arguments would be passed like normal, and the argument to a chained
> method would be the result of the last method call.



Actually, could you clarify how this should be expanded? Should it turn
into:

obj.method3( obj.method2( obj.method1 ) )

or:

obj.method1.method2.method3