String.join()?

I’ve seen some example code that uses what appears to be a string
object with a join method, for example:

(from “rubyisms in rails”)

def category_links(article)
“Posted in " + article.categories.collect { |c|
link_to c.name,
{ :controller=>“articles”, :action => “category”,
:id => c.permalink },
:rel => “tag”
}.join(”, ")
end

But I don’t see api docs in rails or ruby for a join method like this
(obviously there are other join methods).

It appears that this might be a conditional concat (but perhaps
not).

anyone familiar with this?

Joe at CodeGear wrote:

(from “rubyisms in rails”)

Tell such authors not to squander Ruby’s incredibly expressive power
by writing run-on statements. They wouldn’t do that in prose
(right??;).

def category_links(article) "Posted in " + article.categories.collect { |c| link_to c.name, { :controller=>"articles", :action => "category", :id => c.permalink }, :rel => "tag" }.join(", ") end

That is this:

article.categories.collect{ blah blah blah }.join(’, ')

Array#collect returns an Array, and join() makes it into a string.
That’s all.


Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!

On 1/29/07, Joe at CodeGear [email protected] wrote:

          :id => c.permalink },

anyone familiar with this?

It’s an instance method of Array:

irb(main):007:0> Array.instance_methods.grep /join/
=> [“join”]


Zack C.
http://depixelate.com

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs