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?

MB.
  • 4,167
  • 8
  • 52
  • 79
  • 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. – Anand Shah Oct 01 '12 at 13:25
  • @MB. What about `ruby -e "gets.chomp"`? – Josh Voigts Oct 01 '12 at 13:42
  • I'm creating a script someone can download and execute. 'curl http://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. – MB. Oct 01 '12 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?](http://stackoverflow.com/questions/1885525/how-do-i-prompt-a-user-for-confirmation-in-bash-script?lq=1). – the Tin Man Oct 01 '12 at 15:52
  • I'm looking to do it in ruby. – MB. Oct 20 '12 at 04:16

1 Answers1

1

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

MB.
  • 4,167
  • 8
  • 52
  • 79
  • Thanks for this. Here are a couple informative articles I found on the topic from [Thoughbot](https://robots.thoughtbot.com/io-in-ruby) and [StackExchange](http://unix.stackexchange.com/questions/18239/understanding-dev-and-its-subdirs-and-files) – umezo May 19 '16 at 13:37