Forum: Ruby on Rails String.join() ??

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
0997b06e37474faf021df5764626112a?d=identicon&s=25 Joe at CodeGear (Guest)
on 2007-01-29 22:24
(Received via mailing list)
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|
            { :controller=>"articles", :action => "category",
              :id => c.permalink },
              :rel => "tag"
            }.join(", ")

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

anyone familiar with this?
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2007-01-29 22:34
(Received via mailing list)
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

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

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  <-- NOT a blog!!
C4dc94c893471878a105761a9207f29b?d=identicon&s=25 Zack Chandler (Guest)
on 2007-01-29 22:36
(Received via mailing list)
On 1/29/07, Joe at CodeGear <> 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 Chandler
This topic is locked and can not be replied to.