Class: Command

Inherits:
Object
  • Object
show all
Defined in:
lib/riel/command.rb

Overview

-*- ruby -*- !ruby -w

Class Method Summary collapse

Class Method Details

.run(cmd, *args, &blk) ⇒ Object

Runs the given command and arguments, returning the lines of output. If a block is provided, then it will be called with each line of output. If the block takes two arguments, then the line number is also passed to the block.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/riel/command.rb', line 9

def self.run(cmd, *args, &blk)
  lines = Array.new
  
  IO.popen("#{cmd} #{args.join(' ')}") do |io|
    lnum = 0
    io.each_line do |line|
      lines << line
      if blk
        args = [ line ]
        args << lnum if blk.arity > 1
        blk.call(*args)
      end
      lnum += 1
    end
  end
  
  lines
end