Forum: Ruby on Rails myObject.send(column.name) from Agile Development book

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.
78e1e63b1c4789fb757317fe31d362b3?d=identicon&s=25 Shelby Westman (Guest)
on 2005-12-27 05:25
(Received via mailing list)
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 %>
  <tr>
  <% for column in Product.content_columns %>
    <td><%=h product.send(column.name) %></td>
  <% 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
B7aa6f0356566cc801f769c6fc14ba1a?d=identicon&s=25 Francois GORET (Guest)
on 2005-12-27 06:46
(Received via mailing list)
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
4353600bbb6b6e0950e62f7f44e49dd5?d=identicon&s=25 Steven Smith (Guest)
on 2005-12-27 07:04
(Received via mailing list)
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/Co...
Column.html

In the case of the depot app, that would be product.
4353600bbb6b6e0950e62f7f44e49dd5?d=identicon&s=25 Steven Smith (Guest)
on 2005-12-27 18:02
(Received via mailing list)
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.
This topic is locked and can not be replied to.