I know this is a really simple question but my google-fu is failing me. I want to get multiple line user input in Ruby, as gets only gives you one line of input. I want to be able to store this multiple line input into a string so i can output the string to the end of a file. Is there a command available to do this? Thanks in advance!
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Try this:
$/ = "END"
user_input = STDIN.gets
puts user_input
The user can then keep entering a multiple line input and ends his input by typing in END. Keep in mind that IRB doesn't handle this code snippet well, so make sure you use the actual ruby interpreter.
Also the user_input stores the final END typed in by the user so you'll need to strip it out. You can also use any other string in the place of END for the user to indicate that they've finished inputting.