Hey all, A few times in my application I need to print out a comma-delimited list. The easy way is something like: <% for keyword in @keywords -%> <% keyword.text %>, <% end -%> Which would produce something like: keyword1, keyword2, keyword3, And that trailing comma is starting to get to me. I hacked up a working solution that looks like this: <% previous_keyword = nil -%> <% for next_keyword in keywords -%> <% if previous_keyword -%> <%= previous_keyword.text %>, <% end %> <% previous_keyword = next_keyword -%> <% end -%> <%= previous_keyword.text %> This method produces a list like: keyword1, keyword2, keyword3 So it works, but it feels wrong. Is there any built-in way to treat elements of an enumeration differently? I would love something like: <% for keyword in @keywords -%> <% keyword.text %> <% if @keyword.hasMoreElements %> , <% end %> <% end -%> Thanks, Jeff
<%= @keywords.map{|k| k.text}.join('','') %>
Aaron ''Jomdom'' Ransley
2005-Aug-04 17:27 UTC
RE: For Loops and the first or last element
array = [''keyword1'', ''keyword2'', ''keyword3''] array.join('', '') #=> keyword1, keyword2, keyword3 That, something very similar, should do the trick :-) - Aaron ''Jomdom'' Ransley - Web: www.jomdom.net - Mail: jomdom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Jeff Casimir Sent: Thursday, August 04, 2005 10:24 AM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: [Rails] For Loops and the first or last element Hey all, A few times in my application I need to print out a comma-delimited list. The easy way is something like: <% for keyword in @keywords -%> <% keyword.text %>, <% end -%> Which would produce something like: keyword1, keyword2, keyword3, And that trailing comma is starting to get to me. I hacked up a working solution that looks like this: <% previous_keyword = nil -%> <% for next_keyword in keywords -%> <% if previous_keyword -%> <%= previous_keyword.text %>, <% end %> <% previous_keyword = next_keyword -%> <% end -%> <%= previous_keyword.text %> This method produces a list like: keyword1, keyword2, keyword3 So it works, but it feels wrong. Is there any built-in way to treat elements of an enumeration differently? I would love something like: <% for keyword in @keywords -%> <% keyword.text %> <% if @keyword.hasMoreElements %> , <% end %> <% end -%> Thanks, Jeff _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Jeff,> Hey all, > > A few times in my application I need to print out a comma-delimited > list. The easy way is something like: > > <% for keyword in @keywords -%> > <% keyword.text %>, > <% end -%> > > Which would produce something like: > > keyword1, keyword2, keyword3, > > And that trailing comma is starting to get to me.Ruby''s Array#join method to the rescue! <%= @keywords.collect {|k| h(k.text) }.join('', '') %> This calls the code h(k.text) for every element of @keywords and collects that into a new array. The join() method is called for that array, and it outputs each member separated by its argument. Jim -- Jim Menard, jimm-Xhj3G7Rj6JI@public.gmane.org, http://www.io.com/~jimm "Don''t let what you can''t do stand in the way of what you can." -- John Wooden
Jim, Steve, and Matt, Thanks for the responses. I should have known better than to over-simplify my example. I have used .join for simple situations like that and should have thought of it. My real case, however, looks a little something like this: <% for keyword in @keywords -%> <%= link_to "#{keyword.keyword_readable.gsub('' '','' '')}", { :action => ''by_keyword'', :id => keyword }, { :class => ''keyword_forest_'' << keyword.magnitude.to_s } %>, <% end -%> Would it be best to go with Matt''s suggestion to use .each_with_index to do something like this: <% @keywords.each_with_index{|keyword, index| -%> <%= link_to "#{keyword.keyword_readable.gsub('' '','' '')}", { :action => ''by_keyword'', :id => keyword }, { :class => ''keyword_forest_'' << keyword.magnitude.to_s } %> <% unless index == @keywords.size - 1 -%> , <% end -%> <% } -%> Thanks, Jeff> Ruby''s Array#join method to the rescue! > > <%= @keywords.collect {|k| h(k.text) }.join('', '') %> > > This calls the code h(k.text) for every element of @keywords and > collects that into a new array. The join() method is called for that > array, and it outputs each member separated by its argument. > > Jim
Check out http://www.ruby-doc.org/core/classes/Enumerable.html#M001873 each_with_index would let you detect when you are at the last keyword if you know how many keywords you have. -Matt Margolis Jeff Casimir wrote:> Hey all, > > A few times in my application I need to print out a comma-delimited > list. The easy way is something like: > > <% for keyword in @keywords -%> > <% keyword.text %>, > <% end -%> > > Which would produce something like: > > keyword1, keyword2, keyword3, > > And that trailing comma is starting to get to me. I hacked up a > working solution that looks like this: > > <% previous_keyword = nil -%> > <% for next_keyword in keywords -%> > <% if previous_keyword -%> > <%= previous_keyword.text %>, > <% end %> > <% previous_keyword = next_keyword -%> > <% end -%> > <%= previous_keyword.text %> > > This method produces a list like: > > keyword1, keyword2, keyword3 > > So it works, but it feels wrong. Is there any built-in way to treat > elements of an enumeration differently? I would love something like: > > <% for keyword in @keywords -%> > <% keyword.text %> > <% if @keyword.hasMoreElements %> > , > <% end %> > <% end -%> > > Thanks, > Jeff > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Jeff Casimir <jeff-+RlNNtFrnNmT15sufhRIGw@public.gmane.org> writes:> <% for keyword in @keywords -%> > <%= link_to "#{keyword.keyword_readable.gsub('' '','' '')}", > { :action => ''by_keyword'', > :id => keyword > }, > { > :class => ''keyword_forest_'' << keyword.magnitude.to_s > } > %>, > <% end -%>I''d still use join. That''s what it''s there for. <%= @keywords.collect { |keyword| link_to ....... }.join(", ") %> -- doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org