I'm trying to concatenate 2 consecutive strings if any string length is less than 4 characters, but I'm not successful.
我试图连接2个连续的字符串,如果任何字符串长度少于4个字符,但我没有成功。
The code I have so far is:
我到目前为止的代码是:
strings = ['abnhs', 'iuu', 'poioyw', 'tyr', 'mmkaud']
strings.each do |w|
if w.length < 4
temp = w
next
end
w = temp + w
puts w
end
Expected output is:
预期产出是:
abnhs
iuupoioyw
tyrmmkaud
Thanks in advance for any help
在此先感谢您的帮助
1 个解决方案
#1
1
You need to declare the temp variable outside of your each block. Try this:
您需要在每个块之外声明临时变量。尝试这个:
strings = ['abnhs', 'iuu', 'poioyw', 'tyr', 'mmkaud']
temp = ''
strings.each do |w|
if w.length < 4
temp = w
next
end
w = temp + w
puts w
end
#1
1
You need to declare the temp variable outside of your each block. Try this:
您需要在每个块之外声明临时变量。尝试这个:
strings = ['abnhs', 'iuu', 'poioyw', 'tyr', 'mmkaud']
temp = ''
strings.each do |w|
if w.length < 4
temp = w
next
end
w = temp + w
puts w
end