From a57811c4f503913662a1f3354f5622c12be69f05 Mon Sep 17 00:00:00 2001 From: Florian Motlik Date: Mon, 10 Sep 2012 08:52:59 +0200 Subject: [PATCH] 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. --- lib/thor/shell/basic.rb | 2 +- spec/shell/basic_spec.rb | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/thor/shell/basic.rb b/lib/thor/shell/basic.rb index 3c5904b22..276955c1b 100644 --- a/lib/thor/shell/basic.rb +++ b/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) diff --git a/spec/shell/basic_spec.rb b/spec/shell/basic_spec.rb index d8f0f8193..aa1fe9fe1 100644 --- a/spec/shell/basic_spec.rb +++ b/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')