Ruby concatenate strings and add spaces

后端 未结 3 438
不思量自难忘°
不思量自难忘° 2021-02-01 02:03

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\'

3条回答
  •  梦毁少年i
    2021-02-01 02:07

    You can use inject:

    [name,quest,favorite_color,speed].inject("") {|k,v| v.empty? ? k : k << " " << v }.strip
    

提交回复
热议问题