How to join integers with a comma without haveing a trailing comma at the end
If I do this:
users.each do |u|
str += u.id.to_s + ','
end
I will end up with:
1,3,234,234,
Is there a style of looping that won't leave the trailing ','
at the end? I know I can chomp
it off after the loop, but was looking fo开发者_如何学运维r a way to do it w/o having to do that.
Or even shorter:
users.map(&:id).join ','
users.map { |u| u.id.to_s }.join ','
And actually, #join
will do the #to_s
for you, so on second thought:
users.map(&:id).join ','
精彩评论