597821ce12359ab2d95437c8164d5254

I wanted to remove specific e-mail address from array. How to do this better?

I need this in my mailer.

tmp = reply.case.users.map(&:email)
tmp.delete(reply.created_by.email)
tmp.join(',')

@recipients  = "#{tmp}"

Refactorings

No refactoring yet !

E8f0d6e9bc8bca695b3c5bdf75cdcc03

Martin Plöger

September 3, 2009, September 03, 2009 12:27, permalink

1 rating. Login to rate!
tmp = reply.case.users.map(&:email)
tmp.delete(reply.created_by.email)
tmp.join(',')           #ATTENTION DEAD CODE!!! DON'T NEED THAT tmp is NOT changed by this and the result is not used by you. result = tmp.join(',')...

@recipients  = "#{tmp}" # #{tmp} does a tmp.to_s. You could do @recipients = tmp.join(',') otherwise

#======>Refactoring 1:

mails = reply.case.users.map(&:email)
@recipients = mails.reject { |e| e == reply.created_by.email }.join(',')

#======>Refactoring 2:
#If reply.created_by is included in reply.case.users:

@recipients = reply.case.users.reject { |u| u == reply.created_by }.map(&:email).join(',')
#OR
@recipients = (reply.case.users - [reply.created_by]).map(&:email).join(',')
2ddbd54c304df255bdb709f2cd50ffe8

teamco-anthill.blogspot.com

September 3, 2009, September 03, 2009 13:51, permalink

1 rating. Login to rate!

Smaller version

@recipients = reply.case.users.map(&:email).delete(reply.created_by.email)*","
D41d8cd98f00b204e9800998ecf8427e

schnecki

September 3, 2009, September 03, 2009 19:41, permalink

No rating. Login to rate!

@teamco Array#delete doesn't return the resulting array, it returns its argument, for whatever stupid reason

@recipients = (reply.case.users.map(&:email) - [reply.created_by.email]) * ","

# or if reply.created_by is in reply.case.users:
@recipients = (reply.case.users - [reply.created_by]).map(&:email) * ","

Your refactoring





Format Copy from initial code

or Cancel