How can I continue gsub even if there is zero?

I have the following and it explodes when the method โ€œzeroโ€ appears? How can I have no substitution in this case by default?

 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 zero. How can I prevent this in this case?

+3
source share
2 answers

I think you can try adding .to_sto each variable? ex. contact.first_name.to_s. If it is zero, it will become "".

+10
source

I think this would be a good place for a ternary operator.

.gsub("{Title}", contact.title ? contact.title : "default").

if the name of the contact exists, it will use it, and if it will not be used "by default".

0
source

Source: https://habr.com/ru/post/1793787/


All Articles