1

I have a script that runs via a pipe and it doesn't seem to want to capture user input.

This is the simplest example:

  echo "gets.chomp" | ruby
 -:1: private method `chomp' called for nil:NilClass (NoMethodError)

The goal is to confirm an action (Are you sure you want to do this? yes / no: ). How can I capture input when using pipes?

5
  • 1
    What are you trying to accomplish? Give us the broader picture. You are passing in a literal string "gets.chomp" from your shell to ruby interpreter. Commented Oct 1, 2012 at 13:25
  • @MB. What about ruby -e "gets.chomp"? Commented Oct 1, 2012 at 13:42
  • I'm creating a script someone can download and execute. 'curl something.rb | ruby'. Part of the script requires confirmation. I want to ask the user: are you sure you want to continue? yes/no. He writes yes or no and I check the response. Commented Oct 1, 2012 at 13:58
  • You're going about it wrong. You don't need to do it in Ruby when your shell can do it all easily. See "How do I prompt a user for confirmation in bash script?. Commented Oct 1, 2012 at 15:52
  • I'm looking to do it in ruby. Commented Oct 20, 2012 at 4:16

1 Answer 1

1

$stdin = IO.new(IO.sysopen('/dev/tty', 'r'), 'r')

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this. Here are a couple informative articles I found on the topic from Thoughbot and StackExchange

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.