Shorter version for this newbie code...?

Hi all

I have musician_profiles which do different show types (DJ, Live Set,
Instrumental etc.).

With the following code I print them out to screen:

Show types:
<% profile.show_types.each do |s| %>
<%= s.name %>,
<% end %>

For a profile that plays DJ and Live Set this would look like this:
Show types: DJ, Live Set,

I tried to do it shorter:

<%= profile.show_types.each { |s| s.name + “,” } %>

but this only prints something like that:
#ShowType:0x22cd910,#ShowType:0x22cd8d4,

What’s wrong there? And how do I get rid of the comma at the end of the
enumeration in an elegant way? I know there’s join(…), but this as
long as I only get those strange
#ShowType:0x22cd910#ShowType:0x22cd8d4 and so on this doesn’t help
me much. :wink:

Thanks for help,
Joshua

Joshua M. wrote:

Hi all

I have musician_profiles which do different show types (DJ, Live Set,
Instrumental etc.).

With the following code I print them out to screen:

Show types:
<% profile.show_types.each do |s| %>
<%= s.name %>,
<% end %>

For a profile that plays DJ and Live Set this would look like this:
Show types: DJ, Live Set,

I tried to do it shorter:

<%= profile.show_types.each { |s| s.name + “,” } %>

but this only prints something like that:
#ShowType:0x22cd910,#ShowType:0x22cd8d4,

What’s wrong there? And how do I get rid of the comma at the end of the
enumeration in an elegant way? I know there’s join(…), but this as
long as I only get those strange
#ShowType:0x22cd910#ShowType:0x22cd8d4 and so on this doesn’t help
me much. :wink:

Thanks for help,
Joshua

Perhaps you have to convert to string inside the block?
<%= profile.show_types.each { |s| s.name.to_s + “,” } %>

Using join I think it would be:

<% types_joined = profile.show_types.join(,) %>
<%= types_joined %>

Got it! :slight_smile:

<%= profile.show_types.map{ |s| s.name }.join ", " %>

Thanks anyway.

perhaps <%= profile.show_types.collect { |s| s.name +
“,” }.to_s.chop %>

a problem is that <%= displays the return values of what it encloses,
which may not be printable strings



John B.

Try <%= profile.show_types.join(’,’) %>

Hi –

On Sun, 20 Aug 2006, Daniel H. wrote:

<%= s.name %>,
#ShowType:0x22cd910,#ShowType:0x22cd8d4,

What’s wrong there? And how do I get rid of the comma at the end of the
enumeration in an elegant way? I know there’s join(…), but this as
long as I only get those strange
#ShowType:0x22cd910#ShowType:0x22cd8d4 and so on this doesn’t help
me much. :wink:

Try <%= profile.show_types.join(‘,’) %>

I think he wants to harvest the name attributes, like:

<%= profile.show_types.map {|s| s.name }.join(“,”) %>

David


http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack’s][ Web]log
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.

I already got this version (the solution) myself a few posts ago! :wink:
But thanks anyway…

Oops! For some reason I didn’t the second half of the email :stuck_out_tongue: Looks
like he
did what you suggested below, though his follow-up email arrived before
his
original email…

BTW, I have Ruby for Rails and it’s great! Thanks!

Joshua,

Another way I do it is to use rails’ to_sentence.

Show types: <%= profile.show_types.collect(&:name).to_sentence %>

yields

Show types: DJ, Live Set and Instrumental

And to_sentence is much clearer too.

Hope this helps,
Trey

I don’t think you’d be able to use the collect shortcut, but it would
go something like:

<%= profile.show_types.collect{|t| link_to t.name, url_for(:controller
=> ‘show_types’, :action => ‘show’, :id => t.id)}.to_sentence %>

Trey

[email protected] wrote:

Joshua,

Another way I do it is to use rails’ to_sentence.

Show types: <%= profile.show_types.collect(&:name).to_sentence %>

yields

Show types: DJ, Live Set and Instrumental

And to_sentence is much clearer too.

Hope this helps,
Trey

Wow, that is sweet! Thanks! But is there also a way to not only output
the name but also create a link of every name?

Daniel H. wrote:

BTW, I have Ruby for Rails and it’s great! Thanks!

+1

I had a good look at it in a bookshop yesterday, and ended up buying it
(and also Ruby Cookbook, which looks very useful too)

On Sunday 20 August 2006 07:44, [email protected] wrote:
[…]

Justin