Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require "stringio"
- # method I want to test
- def ask(question)
- puts "#{question} [Y/n]"
- STDIN.read.chomp
- end
- def STDIN.capture(default, &block)
- STDIN.reopen "/dev/null" # so we don't get the fucking prompt
- STDIN.ungetbyte(default) # so the default value can be get by STDIN.getc & similar methods
- block.call
- # TODO: how I can get back the original STDIN?
- end
- p STDIN.capture("yes") { ask("Do you hear me?") }
- # => "yes"
Add Comment
Please, Sign In to add comment