D
D
Demigodd2018-10-04 10:07:20
Ruby on Rails
Demigodd, 2018-10-04 10:07:20

How to join two strings?

name1 = "Brad"
name2 = "Pit"

How professionally, in one line of code, to connect these two variables?
If both are not nil then connect like this Brad Pit .
If one of them is nil then write the one that is not nil .
If both are nil then just ''

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Demidenko, 2018-10-04
@Demigodd

['Brad', 'Pit'].compact.join(' ')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question