In my Ruby 2.7 app I want to join an array of strings to have one string separated by commas. As follows:
[company.name, company.street, company.zipcode, company.city]
=> ["Sanford, Reilly and Schmidt", "Hoffmannstr. 186", "84875", "Gebesee"]
Expected result:
["Sanford, Reilly and Schmidt", "Hoffmannstr. 186", "84875 Gebesee"]
Obviously to have such a result I can put an empty string between company.zipcode and company.city and at the end use .join(', ') method like this:
[company.name, company.street, company.zipcode + ' ' + company.city].join(', ')
But honestly this code is smelly for me, is there any better way to achieve the same result?
+to concatenate Strings. Instead use interpolation[company.name, company.street, "#{company.zipcode} #{company.city}"].join(', ')joinbut more powerful. It allows for a second argument (and/or a block):[1,2,3].conjoin(', ', ' and ') # =>"1, 2 and 3"