Module: Cr::Exec

Extended by:
Exec
Included in:
Cr, Exec
Defined in:
lib/cr/exec.rb,
lib/cr/exec/version.rb

Overview

Constant Summary collapse

VERSION =
"0.2.2"

Instance Method Summary collapse

Instance Method Details

#answer(*args, input: nil, **options) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cr/exec.rb', line 41

def answer(*args, input: nil, **options)
  IO.popen(*args, **options) do |pipe|
    if input.is_a?(Array)
      input.each { |cmd| pipe.puts(cmd) }
    elsif input != nil
      pipe.puts(input)
    end
    pipe.close_write
    pipe.read
  end
end

#codeObject



17
18
19
20
21
22
# File 'lib/cr/exec.rb', line 17

def code(...)
  pid, status = Process.wait2(spawn(...))
  status.exitstatus
rescue Errno::ENOENT
  127
end

#each_line(*args, chomp: false, **options, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/cr/exec.rb', line 31

def each_line(*args, chomp: false, **options, &block)
  if block_given?
    IO.popen(*args, **options) do |pipe|
      pipe.each_line(chomp: chomp, &block)
    end
  else
    IO.popen(*args, **options).readlines(chomp: chomp)
  end
end

#output(*args, chomp: true, **options) ⇒ Object



24
25
26
27
28
29
# File 'lib/cr/exec.rb', line 24

def output(*args, chomp: true, **options)
  output = IO.popen(*args, **options).read
  return output.chomp if chomp

  output
end

#runObject



12
13
14
15
# File 'lib/cr/exec.rb', line 12

def run(...)
  pid, status = Process.wait2(spawn(...))
  status
end

#system!(*args, **options) ⇒ Object



57
58
59
# File 'lib/cr/exec.rb', line 57

def system!(*args, **options)
  system(*args, exception: true, **options)
end

#system?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/cr/exec.rb', line 53

def system?(...)
  system(...) ? true : false
end