June
11th,
2011
Playing with the ruby console I am wondering how I can read some input from the console. Ruby ships with the method gets that makes easy to read console input. Just try the following code:
When the console asks for your name just type it. The ruby program will read the console input and will welcome you. You will note that a new line is added after that the variable name is printed out. This is the default behaviour of the gets function. Gets add a new line after any variable that memorize. If this is not desired you can call the method chomp on the gets result and the newline is suppressed.
Change your code as follows and the new line will not be printed anymore: