Forum: Ruby on Rails question about .send()

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8ba20c101499d0ba804612708333bebf?d=identicon&s=25 Garrett Berneche (Guest)
on 2008-10-10 07:44
(Received via mailing list)
I love that I can use .send() to dynamically build the name of the
method I want to call, but how do I build a variable length list of
parameters to go along with it?

I am trying to write a helper for dealing with routing and nested
resources.  Rails sets me up with methods the look like this;

edit_resource_path (resource)
edit_resource_nested_path(resource, nested)
edit_resource_nested_double_nested_path(resource, nested,
double_nested)

...and so on as deep as I have nested my resources.

I can easily construct the name of the method I have to call like
this;
method_name = ("edit_#{object.class.to_s.underscore}_path")

..but to call it with .send() I need to supply various sets of
parameters.  I was hoping I could just stuff them in an array and send
the array, but this didn't work.  any other ideas?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-10-10 08:51
(Received via mailing list)
On Oct 10, 6:43 am, Garrett Berneche <punkrockdontc...@gmail.com>
wrote:
>
> ..but to call it with .send() I need to supply various sets of
> parameters.  I was hoping I could just stuff them in an array and send
> the array, but this didn't work.  any other ideas?

foo.send(:some_method, *array_of_parameters)

Fred
23e4fc3add5443f51e55663cfc245d24?d=identicon&s=25 Erol Fornoles (Guest)
on 2008-10-10 08:55
(Received via mailing list)
On Oct 10, 1:43 pm, Garrett Berneche <punkrockdontc...@gmail.com>
wrote:
> double_nested)
>
> ...and so on as deep as I have nested my resources.
>
> I can easily construct the name of the method I have to call like
> this;
> method_name = ("edit_#{object.class.to_s.underscore}_path")
>
> ..but to call it with .send() I need to supply various sets of
> parameters.  I was hoping I could just stuff them in an array and send
> the array, but this didn't work.  any other ideas?

arg = [param1, param2, ...]
Object.send(method, *arg)
This topic is locked and can not be replied to.