Module: Sys
Instance Method Summary collapse
-
#any_key? ⇒ Boolean
Wait for any key to be pressed.
-
#caller_filename ⇒ Object
Get the caller’s filename for the caller of the function this call is nested in not the function this call is called in.
-
#capture ⇒ Object
Capture STDOUT to a string.
-
#env(var, required: true) ⇒ Object
Get the given environment variable by nam.
-
#getpass ⇒ Object
Read a password from stdin without echoing.
Instance Method Details
#any_key? ⇒ Boolean
Wait for any key to be pressed
40 41 42 43 44 45 46 47 48 |
# File 'lib/nub/sys.rb', line 40 def any_key? begin state = `stty -g` `stty raw -echo -icanon isig` STDIN.getc.chr ensure `stty #{state}` end end |
#caller_filename ⇒ Object
Get the caller’s filename for the caller of the function this call is nested in not the function this call is called in
53 54 55 56 |
# File 'lib/nub/sys.rb', line 53 def caller_filename path = caller_locations(2, 1).first.path return File.basename(path) end |
#capture ⇒ Object
Capture STDOUT to a string
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/nub/sys.rb', line 60 def capture stdout, stderr = StringIO.new, StringIO.new $stdout, $stderr = stdout, stderr result = Proc.new.call $stdout, $stderr = STDOUT, STDERR $stdout.flush $stderr.flush return OpenStruct.new(result: result, stdout: stdout.string, stderr: stderr.string) end |
#env(var, required: true) ⇒ Object
Get the given environment variable by nam
33 34 35 36 37 |
# File 'lib/nub/sys.rb', line 33 def env(var, required:true) value = ENV[var] Log.die("#{var} env variable is required!") if required && !value return value end |
#getpass ⇒ Object
Read a password from stdin without echoing
75 76 77 78 79 80 |
# File 'lib/nub/sys.rb', line 75 def getpass print("Enter Password: ") pass = STDIN.noecho(&:gets).strip puts return pass end |