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

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")
<code>
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
</code>

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?
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
(right??;).

> <code>
> 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
> </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
  http://c2.com/cgi/wiki?ZeekLand  <-- 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 <jmcglynn@codegear.com> 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
http://depixelate.com
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.