The problem you're having is that your input ends with END\n
. Ruby sees the END
, and there's still a \n
left in the buffer. You do successfully set the input record separator back to \n
, so that character is immediately consumed by the second gets
.
You therefore have two easy options:
Set the input record separator to
END\n
(use double quotes in order to have the newline character work):$/ = "END\n"
Clear the buffer with an extra call to
gets
:$/ = 'END' answer = gets gets # Consume extra `\n`
I consider option 1 clearer.
This shows it working on my system using option 1:
$ ruby multiline_input_test.rb this is a multiline awesome input string FTW!! END "this is\n a multiline\n awesome input string\n FTW!!\nEND\n" test "test\n" "magic"