Guest User

Untitled

a guest
Apr 24th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.42 KB | None | 0 0
  1. require "stringio"
  2.  
  3. # method I want to test
  4. def ask(question)
  5. puts "#{question} [Y/n]"
  6. STDIN.read.chomp
  7. end
  8.  
  9. def STDIN.capture(default, &block)
  10. STDIN.reopen "/dev/null" # so we don't get the fucking prompt
  11. STDIN.ungetbyte(default) # so the default value can be get by STDIN.getc & similar methods
  12. block.call
  13. # TODO: how I can get back the original STDIN?
  14. end
  15.  
  16. p STDIN.capture("yes") { ask("Do you hear me?") }
  17. # => "yes"
Add Comment
Please, Sign In to add comment