Permalink
Browse files

Only strip from stdin.gets if it wasn't ended with EOF

When stdin.gets is ended by pressing ctrl-d or the equivalent EOF keys
it returns nil and ask_simply raises a NoMethodException when calling
strip.
  • Loading branch information...
1 parent 54cbeb8 commit a57811c4f503913662a1f3354f5622c12be69f05 @flomotlik committed Sep 10, 2012
Showing with 8 additions and 1 deletion.
  1. +1 −1 lib/thor/shell/basic.rb
  2. +7 −0 spec/shell/basic_spec.rb
View
2 lib/thor/shell/basic.rb
@@ -370,7 +370,7 @@ def as_unicode
def ask_simply(statement, color=nil)
say("#{statement} ", color)
- stdin.gets.strip
+ stdin.gets.tap{|text| text.strip! if text}
end
def ask_filtered(statement, answer_set, *args)
View
7 spec/shell/basic_spec.rb
@@ -24,6 +24,13 @@ def shell
shell.ask("Should I overwrite it?").should == "Sure"
end
+ it "prints a message and returns nil if EOF is sent to stdin" do
+ $stdout.should_receive(:print).with(" ")
+ $stdin.should_receive(:gets).and_return(nil)
+ shell.ask("").should == nil
+ end
+
+
it "prints a message to the user with the available options and determines the correctness of the answer" do
$stdout.should_receive(:print).with('What\'s your favorite Neopolitan flavor? ["strawberry", "chocolate", "vanilla"] ')
$stdin.should_receive(:gets).and_return('chocolate')

0 comments on commit a57811c

Please sign in to comment.