views:

382

answers:

3

I'm collecting all my user's email addresses for a mass mailing like this:

def self.all_email_addresses
  output = ''
  User.all.each{|u| output += u.email + ", " }
      output
end

However, I end up with an extra ", " on the string of email addresses.

How can I get rid of this / is there a better way to get a comma separated list of email addresses?

+15  A: 

Use join:

def self.all_email_addresses
  User.all.collect {|u| u.email}.join ', '
end
giorgian
I jumped the gun a bit, this is what I was going for :P
theIV
Mike Woodhouse
khelll
@khelll: wow, didn't know that!
giorgian
+2  A: 

remove the last two characters

str.chop.chop

Although this does answer the exact question perfectly, I agree that it isn't the best way to solve the actual problem.

DigitalRoss
Yes. To be used with care though, each chop is a brand new string.
giorgian
A: 

Or just "yadayada"[0..-3] will do it.

myferalprofessor