Ruby concatenate strings and add spaces
Posted
by David Oneill
on Stack Overflow
See other posts from Stack Overflow
or by David Oneill
Published on 2010-03-12T18:26:57Z
Indexed on
2010/03/12
18:27 UTC
Read the original article
Hit count: 494
I have 4 string variables name, quest, favorite_color, speed
that might be empty. I want to concatenate them all together, putting spaces between those that aren't empty.
So:
name = 'Tim'
quest = 'destroy'
favorite_color = 'red'
speed = 'fast'
becomes
'Tim destroy red fast'
and
name = 'Steve'
quest = ''
favorite_color = ''
speed = 'slow'
becomes:
'Steve slow'
(Notice: there is only 1 space between 'Steve' and 'slow')
How do I do that (preferably in 1 line).
© Stack Overflow or respective owner