Class: OsCtl::Lib::ProcessList

Inherits:
Array
  • Object
show all
Defined in:
lib/libosctl/process_list.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|process| ... } ⇒ ProcessList

Create an array of system processes

The block can be used to filter out processes for which it returns false.

Parameters:

  • opts (Hash)

    options passed to OsProcess

Yield Parameters:



21
22
23
24
# File 'lib/libosctl/process_list.rb', line 21

def initialize(**, &)
  super()
  list_processes(**, &)
end

Class Method Details

.each(**opts) {|process| ... } ⇒ Object

Iterate over all processes

Parameters:

  • opts (Hash)

    options passed to OsProcess

Yield Parameters:



6
7
8
9
10
11
12
13
# File 'lib/libosctl/process_list.rb', line 6

def self.each(**opts, &block)
  new(**opts) do |p|
    block.call(p)
    next(false)
  end

  nil
end

Instance Method Details

#list_processes(**opts) ⇒ Object (protected)



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/libosctl/process_list.rb', line 28

def list_processes(**opts)
  Dir.foreach('/proc') do |entry|
    next if /^\d+$/ !~ entry || !Dir.exist?(File.join('/proc', entry))

    begin
      p = OsProcess.new(entry.to_i, **opts)
      next if block_given? && !yield(p)

      self << p
    rescue Exceptions::OsProcessNotFound
      next
    end
  end
end