0
What in the world is "gets"
I'm on the user input section of the Ruby tutorial and it has me completely stumped. First off, what's the difference between gets and gets.chomp? Secondly, now that Ruby has the entered information stored, how is it useful to me? (How is it recalled?) I'm quite new to programming and this is quite a bizarre lesson.
1 Answer
+ 2
gets, is a function that gets a string (text) from user input. The chomp function is used to remove the new line character that is take in along with the user input when they press the enter/return key. Usually you would save the input into a variable for later use:
puts "What is your name? "
name = gets
puts "Hello, " + name.chomp
If I input George at the input and didn't use chomp the output would be
Hello, George\n
instead of
Hello, George
You can use the chomp function either directly chained to the gets function or on the string afterwards.
puts = put string
gets = get string