Forum: Ruby on Rails Why doesn't it print out username?

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.
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2006-01-12 14:04
Hi all

I have the following code in an rhtml template:

<%= member.buddies_not_confirmed.each { |b| b.username } %>

I want it to print out

PeterPaulMary etc.

But it prints me

#<Member:0x24a7e0c>#<Member:0x24a7dd0>#<Member:0x24a7d80> etc.

What's wrong here? Sorry for this newbie-like question (although I am a
newbie ;-) ).

Thanks for help.
Josh
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-01-12 14:13
(Received via mailing list)
Joshua Muheim wrote:
> But it prints me
>
> #<Member:0x24a7e0c>#<Member:0x24a7dd0>#<Member:0x24a7d80> etc.
>
> What's wrong here? Sorry for this newbie-like question (although I am a
> newbie ;-) ).
>
The .each method actually returns the original array, not the result of
the block applied to each member.  You either want to do:

<% for buddy in member.buddies_not_confirmed -%>
<%= buddy.username %>
<% end -%>

or possibly:

<%= member.buddies_not_confirmed.collect{|b| b.username}.join %>

Easy mistake to make :-)
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2006-01-12 15:00
> <%= member.buddies_not_confirmed.collect{|b| b.username}.join %>
>
> Easy mistake to make :-)

OK, thanks a lot. :-)
This topic is locked and can not be replied to.