Google+

Improved Prompt Method

Welcome to little random thoughts of Ruby… My topic tonight is about the prompt snippet I gave yesterday. I didn’t realize it, as often happens, but it was so simple, I had to add the feature.

I realized while writing another wrapper for benchmark and profiler, that it is pretty common to add chomp() to our gets. But there may be times when we want what gets, um… well, gets. (Who am I to assume what you may want?) So I give an option. I default to a plain old string that is stripped. But you can easily get the true gets result by asking for true results. Can you guess that the flag to get it could be ‘true’?

improved_prompt.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
class String
=begin rdoc
usage: variable = 'String to display'.prompt
then enter in the string you want to assign to variable.
Alternatively, you can provide a Press Any Key to Continue"
function.
=end
  def prompt(no_chomp = false)
    STDOUT.flush unless STDOUT.sync
    return gets.chomp unless no_chomp
    gets
  end
end

In my irb, Here are a couple of examples…

IRB Session
1
2
3
4
5
6
>> 'Please type something: '.prompt
Please type something: Something
=> "Something"
>> 'Please type something (This will not be stripped): '.prompt(true)
Please type something (This will not be stripped): Something
=> "Something\n

Well, I thought it was pretty cool.