Thursday, April 28, 2011

How to get multiple-line user input in Ruby?

Hey guys,

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!

From stackoverflow
  • 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.

    Javed Ahamed : Thanks! Worked perfectly!

0 comments:

Post a Comment