What does the send() method do on a model object?

All,

I did a scaffold for a model and I ended up with this code in
list.rhtml:

<% for column in TargetList.content_columns %>

<%=h target_list.send(column.name) %>
<% end %>

Where is the “send” method and what does it do?

I can’t find it on ActiveRecord::Base? I assume that it returns a
value based on the column id but I can’t find any documentation on it.

Thanks,
Wes

Hi –

On Tue, 14 Mar 2006, Wes G. wrote:

I can’t find it on ActiveRecord::Base? I assume that it returns a
value based on the column id but I can’t find any documentation on it.

send sends a message to an object. In this case, you’re sending the
name of the column to the object in target_list.

Sending a message is basically equivalent to calling a method. But
since you don’t know the name(s) of the methods in advance (they’re in
the columns), you use send to generalize the process.

It’s a built-in Ruby facility; it’s used in many programs and systems,
not just Rails.

David


David A. Black ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” chapters now available
from Manning Early Access Program! Ruby for Rails

Send is a method from Object that calls the method you specify. That
code looks weird to me, but I haven’t played with scaffolding in a
while.

In normal use, you would probably just call the method directly. In this
case the method is just the field you want to output.

Wes G. wrote:

All,

I did a scaffold for a model and I ended up with this code in
list.rhtml:

<% for column in TargetList.content_columns %>

<%=h target_list.send(column.name) %>
<% end %>

Where is the “send” method and what does it do?

I can’t find it on ActiveRecord::Base? I assume that it returns a
value based on the column id but I can’t find any documentation on it.

Thanks,
Wes

Of course, I didn’t go all the way up the inheritance hierarchy :).

I was kind of hoping it was a mixin so I could learn more about them -
all in due time, I’m sure.

Thanks,
Wes

Ben Sinclair wrote:

Send is a method from Object that calls the method you specify. That
code looks weird to me, but I haven’t played with scaffolding in a
while.

In normal use, you would probably just call the method directly. In this
case the method is just the field you want to output.

Wes G. wrote:

All,

I did a scaffold for a model and I ended up with this code in
list.rhtml:

<% for column in TargetList.content_columns %>

<%=h target_list.send(column.name) %>
<% end %>

Where is the “send” method and what does it do?

I can’t find it on ActiveRecord::Base? I assume that it returns a
value based on the column id but I can’t find any documentation on it.

Thanks,
Wes

Couldn’t you also do something like this ?

<%=h target_list.#{column.name} %>

Untested, but I remember having to call a few methods without knowing
their
names first, and used something similar to this… however… send is
probably more aesthetic than any hack I came up with :slight_smile: