Forum: Ruby on Rails RE: Trying to understand the difference between similaritera

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.
5190273ecab4b93b2d4e7529dcc1f7c6?d=identicon&s=25 unknown (Guest)
on 2006-01-28 01:13
(Received via mailing list)
No I have tried that. It gives me just this output   #
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-01-28 19:03
(Received via mailing list)
Silvy@funmail.com wrote:
> No I have tried that. It gives me just this output   #
>
That's because the <%= %> construct outputs the return value of the
expression inside it.  The .each method actually returns the object it's
called on - which in this case would be @student.addresses, which isn't
a string...  Try:

<td><%= @student.addresses.inject{|s, address| s + "Address:
#{address.addr1}<br />"} %></td>
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-01-28 19:08
(Received via mailing list)
Alex Young wrote:
> Silvy@funmail.com wrote:
>> No I have tried that. It gives me just this output   #
> That's because the <%= %> construct outputs the return value of the
> expression inside it.  The .each method actually returns the object it's
> called on - which in this case would be @student.addresses, which isn't
> a string...  Try:
>
> <td><%= @student.addresses.inject{|s, address| s + "Address:
> #{address.addr1}<br />"} %></td>
>
Oops...  That should probably be

<td><%= @student.addresses.inject(''){|s, address| s + "Address:
#{address.addr1}<br />"} %></td>
This topic is locked and can not be replied to.