Ruby concatenate strings and add spaces Ruby concatenate strings and add spaces ruby ruby

Ruby concatenate strings and add spaces


[name, quest, favorite_color, speed].reject(&:empty?).join(' ')


Try [name,quest,favorite_color,speed].join(' ').squeeze(' ')


You can use inject:

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