ruby - How do I concatenate a string in a while loop? -
i want read lines in loop , concatenate them:
d = "" while s = gets d = d.concat(s) end puts d after cancel loop cntrl+z (on windows), output last string read in loop. tried + , << same result.
you can in two ways way:
d = "" while s = gets d << s end puts d edit: marc-andré lafortune noticed using += not idea, leave << method here.
Comments
Post a Comment