Class: W

Inherits:
Object
  • Object
show all
Defined in:
lib/Unix/w.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string = '') ⇒ W

Returns a new instance of W.



10
11
12
# File 'lib/Unix/w.rb', line 10

def initialize(string = '')
  @time = nil
end

Instance Attribute Details

#loadObject (readonly)

Returns the value of attribute load.



5
6
7
# File 'lib/Unix/w.rb', line 5

def load
  @load
end

#load_1Object (readonly)

Returns the value of attribute load_1.



6
7
8
# File 'lib/Unix/w.rb', line 6

def load_1
  @load_1
end

#load_15Object (readonly)

Returns the value of attribute load_15.



8
9
10
# File 'lib/Unix/w.rb', line 8

def load_15
  @load_15
end

#load_5Object (readonly)

Returns the value of attribute load_5.



7
8
9
# File 'lib/Unix/w.rb', line 7

def load_5
  @load_5
end

#timeObject (readonly)

Returns the value of attribute time.



2
3
4
# File 'lib/Unix/w.rb', line 2

def time
  @time
end

#upObject (readonly)

Returns the value of attribute up.



3
4
5
# File 'lib/Unix/w.rb', line 3

def up
  @up
end

#usersObject (readonly)

Returns the value of attribute users.



4
5
6
# File 'lib/Unix/w.rb', line 4

def users
  @users
end

Instance Method Details

#parse_simple(string) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/Unix/w.rb', line 14

def parse_simple(string)

  regexp = %r{^\s*(\d+:\d+:\d+)\s # current time
up\s+(\d+\sdays,\s*\d+:\d+|\d+:\d+|\d+\s+min), #how long system is up?
\s*(\d+)\suser(?:|s),\s+ # how many users are logged in
load\saverage:\s+(\d+,\d+),\s+(\d+,\d+),\s+(\d+,\d+)\s*$ #load average from last 1 min, 5 mins and 15 mins
}x

  lines = string.split("\n")
  if match = regexp.match(lines[0])
    @time = match[1]
    @up = match[2]
    @users = match[3].to_i
    @load = "#{match[4]}, #{match[5]}, #{match[6]}"
    @load_1 = match[4]
    @load_5 = match[5]
    @load_15 = match[6]
  else
    raise Exception, "Can't parse string >#{lines[0]}<"
  end

end