Module: Sys

Extended by:
Sys
Included in:
Sys
Defined in:
lib/nub/sys.rb

Instance Method Summary collapse

Instance Method Details

#any_key?Boolean

Wait for any key to be pressed

Returns:

  • (Boolean)


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_filenameObject

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

#captureObject

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

Parameters:

  • var (String)

    name of the environment var

  • required (Bool) (defaults to: true)

    require that the variable exists by default



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

#getpassObject

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