Google+

Why Isn't This a Standard Option for Ruby?

Hints for use:

simple useage
1
name = 'Please enter your name'.prompt

I know that each time I want to ask a user for something, I need to write a way to ensure that the user sees the prompt, so that an appropriate answer can be provided. It makes sense to provide a method to do this, as it seems to be a pretty routine thing.

I do this by extending String with method prompt. It is easy to use, and ensures that stdout is flushed so that the user will get the question. If you find it useful, feel free to use it!

Maybe the method will be included some day as a core feature!

prompt.rb
1
2
3
4
5
6
7
class String
  def prompt
    print self
    STDOUT.flush unless STDOUT.sync
    gets
  end
end