myObject.send(column.name) from Agile Development book

I’m studying the Agile Development with Rails book. In the chapter that
first sets up the depot application (page 68), there is this bit of code
from a view:

<% for product in @products %>

<% for column in Product.content_columns %> <%=h product.send(column.name) %> <% end %> etc...

I am trying to absorb both Ruby and rails as fast as I can. So I am
searching both the Ruby API and the Rails API for methods that I don’t
know. When I lookup “send” in the rails API, I don’t find anything.
When I
look it up in the Ruby API, it says that the form is obj.send(symbol
[,args…]). Well, there is no symbol in use here. So I am confused
about
the “send”.

Then, I can’t find a method “name” anywhere. Unlike “human_name”, it is
not
listed as one of the methods of a Column in active record. At least I
can’t
find it.

I know what this code accomplishes, but I wish I understood it better.

Can anyone point me in the right direction?
Thanks,
Shelby

I suggest that you use script/console and irb to find stuff that isn’t
in the
docs.

First go to the directory of your rails app, and do the following:

fg@moo:/opt/www/mart$ script/console
Loading development environment.

Product.columns[1].name
=> “product_category_id”

Product.columns[2].name
=> “reference”

Product.columns[2].name.class
=> String

Product.columns[2].methods.sort
=> ["==", “===”, “=~”, “id”, “send”, “`”, “b64encode”, “blank?”,
“class”, “clone”, “dclone”, “decode64”, “decode_b”, “default”,
“display”,
“dup”, “encode64”, “eql?”, “equal?”, “extend”, “freeze”, “frozen?”,
“hash”,
“human_name”, “id”, “inspect”, “instance_eval”, “instance_of?”,
“instance_variable_get”, “instance_variable_set”, “instance_variables”,
“is_a?”, “kind_of?”, “klass”, “limit”, “load”, “method”, “methods”,
“name”,
“nil?”, “null”, “number?”, “object_id”, “primary”, “primary=”,
“private_methods”, “protected_methods”, “public_methods”,
“remove_subclasses_of”, “require”, “require_gem”,
“require_gem_with_options”,
“require_library_or_gem”, “respond_to?”, “returning”, “send”,
“silence_stderr”, “silence_warnings”, “singleton_methods”,
“subclasses_of”,
“suppress”, “taguri”, “taguri=”, “taint”, “tainted?”, “text?”, “to_a”,
“to_s”, “to_yaml”, “to_yaml_properties”, “to_yaml_style”, “type”,
“type_cast”, “type_cast_code”, “untaint”]

So there’s a ‘name’ method which gives you the name of the column.

fg@moo:/opt/www/mart$ irb
irb(main):001:0> 5.send(:+, 4)
=> 9
irb(main):002:0> 5.send("+", 4)
=> 9

5 is an object, that has a method “+” and accept another object to add
it.
Looks like ‘send’ method accepts either a symbol like :+ or a string “+”
to
name the method.

Good luck,

Francois

You found the right send method. The first argument to send is the
name of the method that you’d like to invoke, followed by any
arguments needed by the method.

http://www.ruby-doc.org/core/classes/Object.html#M001048

Regarding name, it is an attribute of Column, as is default, limit, etc.

http://api.rubyonrails.com/classes/ActiveRecord/ConnectionAdapters/
Column.html

In the case of the depot app, that would be product.

Disregard the last sentence below. It was extraneous.

I had started to say "in the case of the depot app the names of the
methods would be product.title, product.description, etc.