Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Hello Foo STDIN and strip

  • STDIN
  • gets
  • strip

The solution is to remove the newline from the end of the string. We can do this using the strip method:

print "Type in your name: "
person = STDIN.gets.strip

puts "Hello #{person}, how are you?"

Now if we run the program and identify as Bar, the output will look the way we wanted:

Hello Bar, how are you?