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?
question from:
https://stackoverflow.com/questions/65601245/ruby-join-array-of-strings-with-space-between-last-two-elements 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…