Module: Cr::Exec
Overview
Constant Summary collapse
- VERSION =
"0.2.2"
Instance Method Summary collapse
- #answer(*args, input: nil, **options) ⇒ Object
- #code ⇒ Object
- #each_line(*args, chomp: false, **options, &block) ⇒ Object
- #output(*args, chomp: true, **options) ⇒ Object
- #run ⇒ Object
- #system!(*args, **options) ⇒ Object
- #system? ⇒ Boolean
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, **) IO.popen(*args, **) 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 |
#code ⇒ Object
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, **, &block) if block_given? IO.popen(*args, **) do |pipe| pipe.each_line(chomp: chomp, &block) end else IO.popen(*args, **).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, **) output = IO.popen(*args, **).read return output.chomp if chomp output end |
#run ⇒ Object
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, **) system(*args, exception: true, **) end |
#system? ⇒ Boolean
53 54 55 |
# File 'lib/cr/exec.rb', line 53 def system?(...) system(...) ? true : false end |