views:

72

answers:

4
users_allowed_to_be_viewed.map {|u| "#{u.id},"}

but that gives 1,2,3,

What would be a short way to just get something like 1,2,3

+2  A: 
users_allowed_to_be_viewed.join ',' 

ruby-1.8.7-p299 > users_allowed_to_be_viewed = [1,2,3]
   => [1, 2, 3] 
ruby-1.8.7-p299 > users_allowed_to_be_viewed.join ',' 
    => "1,2,3" 
Doon
bah missed the u.id, part.. /sigh.. (see other answers to use the map + join to get what you actually wanted... )
Doon
+3  A: 

an array?

from http://ruby-doc.org/core/classes/Array.html

 array.join(sep=$,) → str

Returns a string created by converting each element of the array to a string, separated by sep.

       [ "a", "b", "c" ].join        #=> "abc"
       [ "a", "b", "c" ].join("-")   #=> "a-b-c"
Roman A. Taycher
+3  A: 
users_allowed_to_be_viewed.map{|u| u.id}.join(",")
Jonas Elfström
This is how I would do it.
Jason Noble
+3  A: 
users_allowed_to_be_viewed.map(&:id).join(',')

Array#join is also aliased to Array#*, although that might make things a little obtuse:

users_allowed_to_be_viewed.map(&:id) * ','
Jörg W Mittag