Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I still do gsub even if there is a nil?

I have the following and it bombs out when the method comes up "nil"? How can I default to no substtution in that case?

 message.gsub("{FirstName}", contact.first_name).
         gsub("{LastName}", contact.last_name).
         gsub("{Title}", contact.title).
         gsub("{Company}", contact.company_name.clear_company).
         gsub("{Colleagues}", colleagues.to_sentence).

For example, I get an error when contact.title is nil. How can I prevent that in that instance?

like image 940
Satchel Avatar asked Dec 16 '22 17:12

Satchel


1 Answers

I guess you can try appending .to_s to each variable? ex. contact.first_name.to_s. If it is nil, then it will become "".

like image 160
Jorge Israel Peña Avatar answered Dec 31 '22 13:12

Jorge Israel Peña