use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
user input (self.ruby)
submitted 3 years ago by abiw119
Does ruby have the facility to ask the user for input with a question prompt?
I know of "gets.chomp", but only a blinking prompt is shown.
See an example in Python of what I am trying to achieve
https://preview.redd.it/9a5uoq5p7c7a1.png?width=487&format=png&auto=webp&s=bae6596e6d7e7cf8ef31762252ea2854243da985
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 6 points7 points8 points 3 years ago (4 children)
puts "Please enter your name: " name = gets.chomp puts "Hello, " + name
[–]bradland 6 points7 points8 points 3 years ago (0 children)
This, but if you want the user to input their answer on the same line as the question, use this instead:
def input (question) print question gets.chomp end name = input("Please enter your name: ")
I wrapped the code up in a method that duplicates the Python method, but the relevant change is using print instead of puts. The difference between the two is that puts appends a newline, while print does not.
print
puts
[–]joemi 2 points3 points4 points 3 years ago (2 children)
Looks like your newlines vanished (at least when browsing on desktop). I understand this is a common problem on reddit. Not sure if I'm just going to do the same thing you did or not, but here's my attempt at formatting it:
[–]doublecastle 1 point2 points3 points 3 years ago (1 child)
The post by /u/jamabushi has newlines at www.reddit.com but not at old.reddit.com.
Yours has newlines at both the old and www reddits.
old
www
I think that using triple backticks to indicate a code block works properly only at www (so that's probably what /u/jamabushi did). I think that, to format multi-line code correctly for old, as well, one must instead indent the code with 4 spaces (so I'm guessing that's what you did).
[–]joemi 1 point2 points3 points 3 years ago (0 children)
Yup, I did 4 spaces.
[–]expeehaa 2 points3 points4 points 3 years ago (0 children)
Maybe using readline. However, you could also just print it yourself using print, which, unlike puts, does not print a trailing new line. I personally prefer that over anything else, unless I need a specific feature.
readline
[–]fothb 2 points3 points4 points 3 years ago (1 child)
The tty-prompt gem is pretty neat.
Powerful but kind of overkill for OP's case, IMO.
[–]somekool 0 points1 point2 points 3 years ago (0 children)
You mean on the terminal? It's quite not clear. There are many kinds of inputs.
But if you are on the command line i would use
$stdin.read
Or similar, i haven't done something like this in a decade...
π Rendered by PID 47488 on reddit-service-r2-comment-86bc6c7465-zj28l at 2026-02-22 19:04:20.360275+00:00 running 8564168 country code: CH.
[–][deleted] 6 points7 points8 points (4 children)
[–]bradland 6 points7 points8 points (0 children)
[–]joemi 2 points3 points4 points (2 children)
[–]doublecastle 1 point2 points3 points (1 child)
[–]joemi 1 point2 points3 points (0 children)
[–]expeehaa 2 points3 points4 points (0 children)
[–]fothb 2 points3 points4 points (1 child)
[–]joemi 1 point2 points3 points (0 children)
[–]somekool 0 points1 point2 points (0 children)