User Input

BrandonDusch580 total contributions
Published Jul 29, 2021Updated Sep 9, 2021
Contribute to Docs
In Ruby, user input is made possible by the #gets
method. During the executing of a Ruby program, when a line with the #gets
method is read, the terminal is primed for input from the user. The input is returned as a string type after the #gets
method is finished.
name = getsputs "My name is #{name}!"
The output would be:
Melanie
My name is Melanie
!
If just the #gets
method is used, the keystroke on the Enter key is submitted with rest of the input, hence why thd !
is on a new line. This can be avoided with the #chomp
method:
name = gets.chompputs "My name is #{name}!"
The output would be:
Brandon
My name is Brandon!
All contributors
- BrandonDusch580 total contributions
- christian.dinh2481 total contributions
- Anonymous contributorAnonymous contributor3077 total contributions
- BrandonDusch
- christian.dinh
- Anonymous contributor
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.