sixarm_ruby_ramp 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,221 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # = Process extensions to help debug Ruby programs.
4
+ #
5
+ # ==Examples
6
+ #
7
+ # p = Process.ps
8
+ # puts p
9
+ # => the results of the 'ps' command for the current process id
10
+ #
11
+ # p = Process.ps(1234)
12
+ # puts p
13
+ # => the results of the 'ps' command for process id 1234
14
+ #
15
+ # p = Process.pss
16
+ # p['%cpu'] => percentage of cpu use, as a float
17
+ # p['%mem'] => percentage of memory use, as a float
18
+ ##
19
+
20
+ module Process
21
+
22
+
23
+ # Get the 'ps' command as one long text string.
24
+ #
25
+ # This is typically useful for logging to a text file.
26
+ #
27
+ # @example
28
+ # pid = 100
29
+ # Process.ps(pid)
30
+ # => "0.0 bfd86194 21:14:51 ..."
31
+ #
32
+ # @return [String] lots of data about the process
33
+
34
+ def self.ps(pid=Process.pid)
35
+ `#{self.ps_command} #{pid.to_i}`
36
+ end
37
+
38
+
39
+ # Get the 'ps' command as a hash of keys and values.
40
+ #
41
+ # @return [Hash<String,String>] the ps command options as keys and values
42
+ #
43
+ # @example
44
+ # pid = 100
45
+ # Process.pss(pid)
46
+ # => {"cp"=>0.0, "esp"=>"bfd86194", "etime"=>"21:14:51", ... }
47
+ # -
48
+ # OPTIMIZE: add dates, times
49
+
50
+ def self.pss(pid=Process.pid)
51
+ ps=self.ps(pid)
52
+ h=Hash[*self.ps_keys.zip(ps.split).flatten]
53
+ h['c'] =h['c'].to_i
54
+ h['cp'] =h['cp'].to_f
55
+ h['egid'] =h['egid'].to_i
56
+ h['egroup'] =h['egroup'].to_i
57
+ h['uid'] =h['uid'].to_i
58
+ h['fgid'] =h['fgid'].to_i
59
+ h['lwp'] =h['lwp'].to_i
60
+ h['ni'] =h['ni'].to_i
61
+ h['nlwp'] =h['nlwp'].to_i
62
+ h['pcpu'] =h['pcpu'].to_f
63
+ h['pgid'] =h['pgid'].to_i
64
+ h['pid'] =h['pid'].to_i
65
+ h['pmem'] =h['pmem'].to_f
66
+ h['ppid'] =h['ppid'].to_i
67
+ h['rgid'] =h['rgid'].to_i
68
+ h['rss'] =h['rss'].to_i
69
+ h['ruid'] =h['ruid'].to_i
70
+ h['sid'] =h['sid'].to_i
71
+ h['sgid'] =h['sgid'].to_i
72
+ h['suid'] =h['suid'].to_i
73
+ self.ps_aliases.each_pair{|key,val| h[key]=h[val]}
74
+ return h
75
+ end
76
+
77
+
78
+ # Get the list of process alias keywords as typically defined by the shell.
79
+ #
80
+ # For example, a shell may consider "%cpu" and "pcpu" to be identical.
81
+ #
82
+ # @example
83
+ # Process::PS_ALIASES_DEFAULT
84
+ # => {"%cpu"=>"pcpu", "sigmask"=>"blocked", "cls"=>"policy", ... }
85
+ #
86
+ # @return [Hash<String,String>] process keyword aliases
87
+
88
+ PS_ALIASES_DEFAULT={
89
+ '%cpu'=>'pcpu',
90
+ '%mem'=>'pmem',
91
+ 'sig_block'=>'blocked',
92
+ 'sigmask'=>'blocked',
93
+ 'sig_catch'=>'caught',
94
+ 'sigcatch'=>'caught',
95
+ 'cls'=>'class',
96
+ 'cls'=>'policy',
97
+ 'cputime'=>'time',
98
+ 'gid'=>'egid',
99
+ 'group'=>'egroup',
100
+ 'uid'=>'euid',
101
+ 'uname'=>'euser',
102
+ 'user'=>'euser',
103
+ 'flag'=>'f',
104
+ 'flags'=>'f',
105
+ 'fsuid'=>'fuid',
106
+ 'sig_ignore'=>'ignored',
107
+ 'sigignore'=>'ignored',
108
+ 'spid'=>'lwp',
109
+ 'tid'=>'lwp',
110
+ 'nice'=>'ni',
111
+ 'thcount'=>'nlwp',
112
+ 'sig'=>'pending',
113
+ 'sig_pend'=>'pending',
114
+ 'pgrp'=>'pgid',
115
+ 'rssize'=>'rss',
116
+ 'rsz'=>'rss',
117
+ 'state'=>'s',
118
+ 'sess'=>'sid',
119
+ 'session'=>'sid',
120
+ 'svgid'=>'sgid',
121
+ 'tt'=>'tname',
122
+ 'tty'=>'tname',
123
+ 'vsz'=>'vsize'
124
+ }
125
+
126
+
127
+ # Get the list of process alias keywords as typically defined by the shell.
128
+ #
129
+ # For example, a shell may consider "%cpu" and "pcpu" to be identical.
130
+ #
131
+ # @example
132
+ # Process.ps_aliases
133
+ # => {"%cpu"=>"pcpu", "sigmask"=>"blocked", "cls"=>"policy", ... }
134
+ #
135
+ # @return [Hash<String,String>] process keyword aliases
136
+
137
+ def self.ps_aliases
138
+ @@ps_aliases||=PS_ALIASES_DEFAULT
139
+ end
140
+
141
+
142
+ # Set the list of process alias keywords.
143
+ #
144
+ # For example, a shell may consider "%cpu" and "pcpu" to be identical.
145
+ #
146
+ # @example
147
+ # Process::ps_aliases={ {"%cpu"=>"pcpu", "sigmask"=>"blocked", "cls"=>"policy" }
148
+ # => {"%cpu"=>"pcpu", "sigmask"=>"blocked", "cls"=>"policy"}
149
+ #
150
+ # @param [Hash<String,String>] aliases
151
+ # @return [Hash<String,String>] aliases
152
+
153
+ def self.ps_aliases=(aliases)
154
+ @@ps_aliases=aliases
155
+ end
156
+
157
+
158
+ # The list of process keywords.
159
+
160
+ PS_KEYS_DEFAULT=%w'blocked bsdtime c caught class cp egid egroup eip esp etime euid euser f fgid fgroup fuid fuser group ignored label lwp ni nlwp nwchan pending pcpu pgid pid pmem ppid pri psr rgid rgroup rss rtprio ruid ruser s sched sgi_p sgid sgroup sid sig size stackp start_time stat suid suser sz time tname tpgid vsize wchan'
161
+
162
+
163
+ # Get the list of process keywords.
164
+ #
165
+ # @example
166
+ # Process.ps_keys
167
+ # => ["blocked","group","pending","size"]
168
+ #
169
+ # @return [Array<String>] the list of process keywords.
170
+
171
+
172
+ def self.ps_keys
173
+ @@ps_keys||=PS_KEYS_DEFAULT
174
+ end
175
+
176
+
177
+ # Set the list of process keywords.
178
+ #
179
+ # @example
180
+ # Process.ps_keys = ["blocked","group","pending","size"]
181
+ #
182
+ # @param [Array<String>] keywords
183
+ # @return [Array<String>] keywords
184
+
185
+ def self.ps_keys=(keys)
186
+ @@ps_keys=keys
187
+ end
188
+
189
+
190
+ # The process command, i.e. what the sytem will call for the "ps" command.
191
+
192
+ PS_COMMAND_DEFAULT='ps h ww -o "'+self.ps_keys.join(',')+'"'
193
+
194
+
195
+ # Get the process command, i.e. what the sytem will call for the "ps" command.
196
+ #
197
+ # @example
198
+ # Process.ps_command
199
+ # => "ps h ww -o blocked,group,pending,size"
200
+ #
201
+ # @return [String] the process command
202
+
203
+ def self.ps_command
204
+ @@ps_command||=PS_COMMAND_DEFAULT
205
+ end
206
+
207
+
208
+ # Set the process command, i.e. what the sytem will call for the "ps" command.
209
+ #
210
+ # @example
211
+ # Process.ps_command = "ps h ww -o blocked,group,pending,size"
212
+ #
213
+ # @param [String] the process command
214
+ # @return [String] the process command
215
+
216
+ def self.ps_command=(command)
217
+ @@ps_command=command
218
+ end
219
+
220
+
221
+ end
@@ -0,0 +1,230 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ # String extensions
4
+
5
+ class String
6
+
7
+
8
+ # @return [String] self, with words capitalized
9
+ # @example
10
+ # "foo goo hoo".capitalize_words
11
+ # => "Foo Goo Hoo"
12
+
13
+ def capitalize_words
14
+ split(/\b/).map{|word| word.capitalize }.join
15
+ end
16
+
17
+
18
+ # @return [Array<String>] an array that is the string split into words, i.e. split(\W*\b\*)
19
+ # @example
20
+ # "foo goo hoo".words
21
+ # => ["foo", "goo", "hoo"]
22
+
23
+ def words
24
+ split(/\W*\b\W*/)
25
+ end
26
+
27
+
28
+ # @return [Array<String>] an array that is the string split at tabs, i.e. split(/\t/)
29
+ # @example
30
+ # "foo\tgoo\thoo".split_tab
31
+ # => ["foo", "goo", "hoo"]
32
+
33
+ def split_tab
34
+ split(/\t/)
35
+ end
36
+
37
+
38
+ # This is useful to split a TSV (Tab Separated Values) string
39
+ # into an array of rows, and each row into an array of fields.
40
+ #
41
+ # @return [Array<String>] an array that is the string split at newlines, then tabs.
42
+ #
43
+ # @example
44
+ # "foo\tgoo\thoo\n"ioo\tjoo\tkoo\nloo\tmoo\tnoo".split_tsv
45
+ # => [["foo", "goo", "hoo"], ["ioo", "joo", "koo"], ["loo", "moo", "noo"]]
46
+
47
+ def split_tsv
48
+ split(/\n/).map{|line| line.split(/\t/)}
49
+ end
50
+
51
+
52
+ # @return [String] self in lowercase,
53
+ # with any non-word-characters
54
+ # replaced with single underscores (aka low dashes).
55
+ #
56
+ # @example
57
+ # 'Foo Goo Hoo' => 'foo_goo_hoo'
58
+ # 'Foo***Goo***Hoo' => 'foo_goo_hoo'
59
+
60
+ def lowcase
61
+ downcase.gsub(/[_\W]+/,'_')
62
+ end
63
+
64
+
65
+ # @return [String] the string as an XML id, which is the same as #lowcase
66
+ #
67
+ # @example
68
+ # "Foo Hoo Goo" => 'foo_goo_hoo'
69
+ # "Foo***Goo***Hoo" => 'foo_goo_hoo'
70
+
71
+ def to_xid
72
+ self.lowcase
73
+ end
74
+
75
+ # Ruby String#to_class method to convert from a String to a class
76
+ #
77
+ # From Mirage at http://infovore.org/archives/2006/08/02/getting-a-class-object-in-ruby-from-a-string-containing-that-classes-name/
78
+ #
79
+ # @return [Class] the string converted to a class
80
+
81
+ def to_class
82
+ split('::').inject(Kernel) {|scope, const_name| scope.const_get(const_name)}
83
+ end
84
+
85
+
86
+ # Increment the rightmost natural number
87
+ #
88
+ # @return [String] the string with an incremented rightmost number
89
+ #
90
+ # @example
91
+ # 'foo5bar'.increment => 'foo4bar'
92
+ # 'foo5bar'.increment(3) => 'foo8bar'
93
+ # 'foo9bar'.increment => 'foo10bar'
94
+ #
95
+ # @see String#decrement
96
+
97
+ def increment(step=1)
98
+ self=~/\d+/ ? $`+($&.to_i+step).to_s+$' : self
99
+ end
100
+
101
+
102
+ # Decrement the rightmost natural number
103
+ #
104
+ # @return [String] the string with a decremented rightmost number
105
+ #
106
+ # @example
107
+ # 'foo5bar'.decrement => 'foo4bar'
108
+ # 'foo5bar'.decrement(3) => 'foo2bar'
109
+ # 'foo10bar'.derement => 'foo9bar'
110
+ #
111
+ # @see String#increment
112
+
113
+ def decrement(step=1)
114
+ self=~/\d+/ ? $`+($&.to_i-step).to_s+$' : self
115
+ end
116
+
117
+
118
+ # @return [String] the previous character, with a changed flag and carry flag
119
+ #
120
+ # @example
121
+ # String.prev_char('n') => 'm', true, false # change
122
+ # String.prev_char('a') => 'z', true, true # change & carry
123
+ # String.prev_char('6') => '5', true, false # change
124
+ # String.prev_char('0') => '9', true, true # change & carry
125
+ # String.prev_char('-') => '-', false, false # unchanged
126
+
127
+ def self.prev_char(chr) #=> prev_char, changed_flag, carry_flag
128
+ case chr
129
+ when '1'..'9', 'B'..'Z', 'b'..'z'
130
+ pos=(chr.respond_to?(:ord) ? chr.ord : chr[0])
131
+ return (pos-1).chr, true, false
132
+ when '0'
133
+ return '9', true, true
134
+ when 'A'
135
+ return 'Z', true, true
136
+ when 'a'
137
+ return 'z', true, true
138
+ else
139
+ return chr, false, false
140
+ end
141
+ end
142
+
143
+ # @return [String] the previous string
144
+ #
145
+ # @see String#next
146
+ #
147
+ # @example
148
+ # '888'.prev => '887'
149
+ # 'n'.prev => 'm'
150
+ # 'N'.prev => 'M'
151
+ #
152
+ # @example with carry
153
+ # '880'.prev => '879'
154
+ # 'nna'.prev => 'nmz'
155
+ # 'NNA'.prev => 'NMZ'
156
+ # 'nn0aA'.prev => 'nm9zZ'
157
+
158
+ def prev
159
+ self.clone.prev!
160
+ end
161
+
162
+
163
+ # Do String#prev in place
164
+ #
165
+ # @return [String] self
166
+
167
+ def prev!
168
+ return self if length==0
169
+ index=length-1 # rightmost
170
+ while true do
171
+ chr=self[index].chr
172
+ prev_chr,changed_flag,carry_flag=String.prev_char(chr)
173
+ return self if !changed_flag
174
+ self[index]=prev_chr
175
+ return self if !carry_flag
176
+ index-=1
177
+ return nil if index<0
178
+ end
179
+ return self
180
+ end
181
+
182
+ alias pred prev # String#pred : predecessor :: String#succ : successor
183
+ alias pred! prev!
184
+
185
+ class << self
186
+ alias_method :pred_char, :prev_char
187
+ end
188
+
189
+ # Helpful constants
190
+
191
+ LOWERCASE_ENGLISH_CHARS = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
192
+ UPPERCASE_ENGLISH_CHARS = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
193
+
194
+
195
+ ##
196
+ #
197
+ # Lorem Ipsum random text generator
198
+ #
199
+ ##
200
+
201
+ # @return [Integer[ a random length suitable for a "lorem ipsum" string.
202
+ #
203
+ # This method uses 1+rand(10)
204
+ #
205
+ # @example
206
+ # String.lorem_length => 3
207
+ # String.lorem_length => 9
208
+ # String.lorem_length => 5
209
+
210
+ def self.lorem_length
211
+ 1+rand(10)
212
+ end
213
+
214
+ # @return [String] a random string suitable for "lorem ipsum" text.
215
+ #
216
+ # @example
217
+ # String.lorem => "galkjadscals"
218
+ # String.lorem(4) => "qtgf"
219
+ #
220
+ # This method chooses from lowercase letters a-z.
221
+ #
222
+ # This method defaults to length = self.lorem_length.
223
+
224
+ def self.lorem(length=self.lorem_length)
225
+ ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'].choices(length).join
226
+ end
227
+
228
+
229
+ end
230
+
@@ -0,0 +1,29 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ # Symbol extensions
4
+
5
+ class Symbol
6
+
7
+ # Compare this symbol to another symbol.
8
+ #
9
+ # @example Less than
10
+ # :foo <=> :goo
11
+ # => -1
12
+ #
13
+ # @example Equal
14
+ # :foo <=> :foo
15
+ # => 0
16
+ #
17
+ # @example Greater than
18
+ # :foo <=> :bar
19
+ # => 1
20
+ #
21
+ # @return [-1,0,1] -1 if this is < that, 0
22
+
23
+ include Comparable
24
+ def <=>(that)
25
+ self.to_s<=>that.to_s
26
+ end
27
+
28
+ end
29
+
@@ -0,0 +1,61 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ # Time extensions
4
+
5
+ class Time
6
+
7
+
8
+ # @return [String] a time stamp string in standard format: "YYYY-MM-DD HH:MM:SSZ"
9
+ #
10
+ # This standard format is specified in IETF RFC 3339 and ISO 8601.
11
+ #
12
+ # @see http://www.ietf.org/rfc/rfc3339.txt
13
+ #
14
+ # @example
15
+ # Time.now.stamp
16
+ # => "2010-12-31 12:59:59Z"
17
+
18
+ def stamp
19
+ getutc.strftime('%Y-%m-%d %H:%M:%SZ')
20
+ end
21
+
22
+
23
+ # Shorthand for Time.now.stamp
24
+ #
25
+ # @example
26
+ # Time.stamp
27
+ # => "2010-12-31 12:59:59Z"
28
+ #
29
+ # @return [String] Time.now.stamp
30
+
31
+ def self.stamp
32
+ now.stamp
33
+ end
34
+
35
+
36
+ # @return [String] time packed into a short string: "YYYYMMDDHHMMSS"
37
+ #
38
+ # The time is converted to UTC.
39
+ #
40
+ # @example
41
+ # Time.now.pack
42
+ # => "20101231125959"
43
+
44
+ def pack
45
+ getutc.strftime('%Y%m%d%H%M%S')
46
+ end
47
+
48
+
49
+ # Shorthand for Time.now.pack
50
+ #
51
+ # @example
52
+ # Time.pack
53
+ # => "20101231125959"
54
+ #
55
+ # @return [String] Time.now.pack
56
+
57
+ def self.pack
58
+ now.pack
59
+ end
60
+
61
+ end