webget_ruby_ramp 1.8.0 → 1.8.2
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.
- data.tar.gz.sig +2 -2
- data/README.rdoc +24 -7
- data/lib/webget_ruby_ramp.rb +2 -2
- data/lib/webget_ruby_ramp/array.rb +56 -43
- data/lib/webget_ruby_ramp/class.rb +3 -1
- data/lib/webget_ruby_ramp/csv.rb +4 -4
- data/lib/webget_ruby_ramp/date.rb +12 -12
- data/lib/webget_ruby_ramp/enumerable.rb +101 -46
- data/lib/webget_ruby_ramp/file.rb +2 -2
- data/lib/webget_ruby_ramp/fixnum.rb +4 -7
- data/lib/webget_ruby_ramp/hash.rb +18 -15
- data/lib/webget_ruby_ramp/integer.rb +8 -7
- data/lib/webget_ruby_ramp/io.rb +24 -20
- data/lib/webget_ruby_ramp/kernel.rb +32 -0
- data/lib/webget_ruby_ramp/math.rb +10 -2
- data/lib/webget_ruby_ramp/nil.rb +5 -2
- data/lib/webget_ruby_ramp/numeric.rb +5 -71
- data/lib/webget_ruby_ramp/object.rb +4 -3
- data/lib/webget_ruby_ramp/process.rb +54 -7
- data/lib/webget_ruby_ramp/string.rb +54 -24
- data/lib/webget_ruby_ramp/symbol.rb +16 -0
- data/lib/webget_ruby_ramp/time.rb +19 -11
- data/lib/webget_ruby_ramp/xml.rb +30 -20
- data/lib/webget_ruby_ramp/yaml.rb +5 -4
- data/test/webget_ruby_ramp/numeric_test.rb +0 -60
- data/test/webget_ruby_ramp/string_test.rb +0 -5
- metadata +2 -2
- metadata.gz.sig +1 -1
@@ -6,16 +6,17 @@ class Integer
|
|
6
6
|
|
7
7
|
# Syntactic sugar to yield n times to a block.
|
8
8
|
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
# ==Example
|
12
|
-
# 3.maps{rand} => [0.0248131784304143, 0.814666170190905, 0.15812816258206]
|
13
|
-
#
|
14
|
-
# ==Parallel to Integer#times
|
15
|
-
#
|
9
|
+
# Comparison to Integer#times:
|
16
10
|
# Integer#maps is similar to Integer#times except that the output from each
|
17
11
|
# call to the block is captured in an array element and that array is
|
18
12
|
# returned to the calling code.
|
13
|
+
#
|
14
|
+
# @return an array of any results
|
15
|
+
#
|
16
|
+
# @example Generate an array of three random numbers
|
17
|
+
# 3.maps{rand}
|
18
|
+
# => [0.0248131784304143, 0.814666170190905, 0.15812816258206]
|
19
|
+
#
|
19
20
|
|
20
21
|
def maps
|
21
22
|
return (0...self).map{|item| yield item}
|
data/lib/webget_ruby_ramp/io.rb
CHANGED
@@ -8,55 +8,59 @@ class IO
|
|
8
8
|
# Reads the entire file specified by name as individual lines as with IO#readlines,
|
9
9
|
# and returns those lines in an array of rows, where each row is an array of fields.
|
10
10
|
#
|
11
|
-
#
|
11
|
+
# @return [Array<Array<String>>] an array of rows, where each row is an array of fields.
|
12
|
+
#
|
13
|
+
# @example
|
12
14
|
# IO.readrows("my.tsv")
|
13
15
|
# => [["A1","B1","C1"],["A2","B2","C2"],["A3","B3","C3"]]
|
14
16
|
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
17
|
+
# @param [String] filename
|
18
|
+
# @param [Hash] options
|
19
|
+
# - Rows are separated by _row_ option, which defaults to Ruby's record separator $/ or "\n"
|
20
|
+
# - Cols are separated by _col_ option, which defaults to Ruby's string split separator $; or "\t"
|
18
21
|
#
|
19
|
-
#
|
22
|
+
# @example with options suitable for a file using TSV (Tab Separated Values)
|
20
23
|
# IO.readrows("my.tsv", :row=>"\n", :col=>"\t")
|
21
24
|
#
|
22
25
|
# Note: the col option is sent along to String#split, so can be a string or a regexp.
|
23
26
|
#
|
24
|
-
#
|
27
|
+
# @see
|
25
28
|
# - File#readline
|
26
29
|
# - File#readlines
|
27
30
|
# - File#readrow
|
28
31
|
|
29
|
-
def IO.readrows(
|
32
|
+
def IO.readrows(filename, options={})
|
30
33
|
row_sep||=options[:row]||$/||"\n"
|
31
34
|
col_sep||=options[:col]||$;||"\t"
|
32
|
-
return IO.readlines(
|
35
|
+
return IO.readlines(filename, row_sep).map{|line| line.chomp(row_sep).split(col_sep)}
|
33
36
|
end
|
34
37
|
|
35
38
|
|
36
39
|
# Read a line as with IO#readline and return the line as a row of fields.
|
37
40
|
#
|
38
|
-
#
|
41
|
+
# Note: the col option is sent along to String#split, so can be a string or a regexp.
|
42
|
+
#
|
43
|
+
# @return [Array<String>] fields
|
44
|
+
#
|
45
|
+
# @param [Hash] options
|
46
|
+
# - Rows are separated by _row_ option, which defaults to Ruby's record separator $/ or "\n"
|
47
|
+
# - Cols are separated by _col_ option, which defaults to Ruby's string split separator $; or "\t"
|
48
|
+
#
|
49
|
+
# @example
|
39
50
|
# file = File.new("my.tsv")
|
40
51
|
# file.readrow() => ["A1","B1","C1"]
|
41
52
|
# file.readrow() => ["A2","B2","C2"]
|
42
53
|
# file.readrow() => ["A3","B3","C3"]]
|
43
54
|
#
|
44
|
-
#
|
45
|
-
# - Rows are separated by _row_ option, which defaults to Ruby's record separator $/ or "\n"
|
46
|
-
# - Cols are separated by _col_ option, which defaults to Ruby's string split separator $; or "\t"
|
47
|
-
#
|
48
|
-
# ==Example with options suitable for a file using TSV (Tab Separated Values)
|
55
|
+
# @example with options suitable for a file using TSV (Tab Separated Values)
|
49
56
|
# file = File.new("my.tsv")
|
50
57
|
# file.readrow(:row=>"\n", :col=>"\t") => ["A1","B1","C1"]
|
51
58
|
# file.readrow(:row=>"\n", :col=>"\t") => ["A2","B2","C2"]
|
52
59
|
# file.readrow(:row=>"\n", :col=>"\t") => ["A3","B3","C3"]
|
53
60
|
#
|
54
|
-
#
|
55
|
-
#
|
56
|
-
#
|
57
|
-
# - File#readline
|
58
|
-
# - File#readlines
|
59
|
-
# - File#readrows
|
61
|
+
# @see File#readline
|
62
|
+
# @see File#readlines
|
63
|
+
# @see File#readrows
|
60
64
|
|
61
65
|
def readrow(options={})
|
62
66
|
row_sep||=options[:row]||$/||"\n"
|
@@ -14,6 +14,18 @@ module Kernel
|
|
14
14
|
#-
|
15
15
|
# Make this fast because its often doing logging & reporting.
|
16
16
|
# Inline this and use $1 because it's empirically faster than /1
|
17
|
+
#
|
18
|
+
# These two methods are always equal:
|
19
|
+
# caller_method_name(0) === my_method_name
|
20
|
+
#
|
21
|
+
# @example
|
22
|
+
# def foo
|
23
|
+
# puts my_method_name
|
24
|
+
# end
|
25
|
+
# foo
|
26
|
+
# => "foo"
|
27
|
+
#
|
28
|
+
# @return [String] my method name
|
17
29
|
|
18
30
|
def my_method_name
|
19
31
|
caller[0] =~ /`([^']*)'/ and $1
|
@@ -27,6 +39,20 @@ module Kernel
|
|
27
39
|
#-
|
28
40
|
# Make this fast because its often doing logging & reporting.
|
29
41
|
# Inline this and use $1 because it's empirically faster than /1
|
42
|
+
#
|
43
|
+
# These two methods are always equal:
|
44
|
+
# caller_method_name(0) === my_method_name
|
45
|
+
#
|
46
|
+
# @example
|
47
|
+
# def foo
|
48
|
+
# puts caller_method_name(0)
|
49
|
+
# puts caller_method_name(1)
|
50
|
+
# end
|
51
|
+
# =>
|
52
|
+
# "foo"
|
53
|
+
# "irb_binding"
|
54
|
+
#
|
55
|
+
# @return [String] the method name of the caller at the index
|
30
56
|
|
31
57
|
def caller_method_name(caller_index=0)
|
32
58
|
caller[caller_index] =~ /`([^']*)'/ and $1
|
@@ -38,6 +64,12 @@ module Kernel
|
|
38
64
|
# Created by Graeme Mathieson.
|
39
65
|
#
|
40
66
|
# See http://rha7dotcom.blogspot.com/2008/07/ruby-and-rails-how-to-get-pp-pretty.html
|
67
|
+
#
|
68
|
+
# @example
|
69
|
+
# pp_s(["foo","goo"])
|
70
|
+
# => "[\"foo\", \"goo\"]\n"
|
71
|
+
#
|
72
|
+
# @return [String] a pretty print string of the params
|
41
73
|
|
42
74
|
def pp_s(*objs)
|
43
75
|
str = StringIO.new
|
@@ -5,14 +5,22 @@
|
|
5
5
|
module Math
|
6
6
|
|
7
7
|
|
8
|
-
#
|
8
|
+
# @return [Float] the natural log of _num_
|
9
|
+
#
|
10
|
+
# @example
|
11
|
+
# Math.ln(2.719)
|
12
|
+
# => 1.00
|
9
13
|
|
10
14
|
def Math.ln(num)
|
11
15
|
Math.log(num)
|
12
16
|
end
|
13
17
|
|
14
18
|
|
15
|
-
#
|
19
|
+
# @return [Float] the log of _num_ in base _base_
|
20
|
+
#
|
21
|
+
# @example
|
22
|
+
# Math.logn(10000,10)
|
23
|
+
# => 4.00
|
16
24
|
|
17
25
|
def Math.logn(num,base)
|
18
26
|
Math.ln(num)/Math.ln(base)
|
data/lib/webget_ruby_ramp/nil.rb
CHANGED
@@ -4,9 +4,10 @@
|
|
4
4
|
|
5
5
|
class Numeric
|
6
6
|
|
7
|
-
|
7
|
+
|
8
|
+
# @return 0 if the given flag is any of: nil, false, 0, [], {}
|
8
9
|
#
|
9
|
-
#
|
10
|
+
# @example
|
10
11
|
# 3.if(true) => 3
|
11
12
|
# 3.if(false) => 0
|
12
13
|
#
|
@@ -16,9 +17,9 @@ class Numeric
|
|
16
17
|
end
|
17
18
|
|
18
19
|
|
19
|
-
#
|
20
|
+
# @return self if flag is nil, false, 0, [], {}; otherwise return 0.
|
20
21
|
#
|
21
|
-
#
|
22
|
+
# @example
|
22
23
|
# 3.unless(true) => 0
|
23
24
|
# 3.unless(false) => 3
|
24
25
|
|
@@ -27,71 +28,4 @@ class Numeric
|
|
27
28
|
end
|
28
29
|
|
29
30
|
|
30
|
-
###
|
31
|
-
#
|
32
|
-
# Metric conversions
|
33
|
-
#
|
34
|
-
###
|
35
|
-
|
36
|
-
# Return self / 10^15
|
37
|
-
def peta
|
38
|
-
self/1000000000000000
|
39
|
-
end
|
40
|
-
|
41
|
-
# Return self / 10^12
|
42
|
-
def tera
|
43
|
-
self/1000000000000
|
44
|
-
end
|
45
|
-
|
46
|
-
# Return self / 10^9
|
47
|
-
def giga
|
48
|
-
self/1000000000
|
49
|
-
end
|
50
|
-
|
51
|
-
# Return self / 10^6
|
52
|
-
def mega
|
53
|
-
self/1000000
|
54
|
-
end
|
55
|
-
|
56
|
-
# Return self / 10^3
|
57
|
-
def kilo
|
58
|
-
self/1000
|
59
|
-
end
|
60
|
-
|
61
|
-
# Return self / 10^2
|
62
|
-
def hecto
|
63
|
-
self/100
|
64
|
-
end
|
65
|
-
|
66
|
-
# Return self / 10
|
67
|
-
def deka
|
68
|
-
self/10
|
69
|
-
end
|
70
|
-
|
71
|
-
# Return self * 10
|
72
|
-
def deci
|
73
|
-
self*10
|
74
|
-
end
|
75
|
-
|
76
|
-
# Return self * 10^2
|
77
|
-
def centi
|
78
|
-
self*100
|
79
|
-
end
|
80
|
-
|
81
|
-
# Return self * 10^3
|
82
|
-
def milli
|
83
|
-
self*1000
|
84
|
-
end
|
85
|
-
|
86
|
-
# Return self * 10^6
|
87
|
-
def micro
|
88
|
-
self*1000000
|
89
|
-
end
|
90
|
-
|
91
|
-
# Return self * 10^9
|
92
|
-
def nano
|
93
|
-
self*1000000000
|
94
|
-
end
|
95
|
-
|
96
|
-
|
97
31
|
end
|
@@ -6,15 +6,16 @@ class Object
|
|
6
6
|
|
7
7
|
# Syntactic sugar for arrays.
|
8
8
|
#
|
9
|
-
#
|
9
|
+
# Definition:
|
10
10
|
# object.in? array === array.include? object
|
11
11
|
#
|
12
|
-
#
|
12
|
+
# @example
|
13
13
|
# array=['a','b','c']
|
14
14
|
# object='b'
|
15
15
|
# object.in? array
|
16
16
|
# => true
|
17
|
-
|
17
|
+
#
|
18
|
+
# @return [Boolean] true iff this object is included in the array.
|
18
19
|
def in?(array)
|
19
20
|
array.include?(self)
|
20
21
|
end
|
@@ -23,6 +23,13 @@ module Process
|
|
23
23
|
# Get the 'ps' command as one long text string.
|
24
24
|
#
|
25
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
|
26
33
|
|
27
34
|
def self.ps(pid=Process.pid)
|
28
35
|
`#{self.ps_command} #{pid.to_i}`
|
@@ -30,6 +37,13 @@ module Process
|
|
30
37
|
|
31
38
|
|
32
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", ... }
|
33
47
|
# -
|
34
48
|
# OPTIMIZE: add dates, times
|
35
49
|
|
@@ -60,9 +74,16 @@ module Process
|
|
60
74
|
return h
|
61
75
|
end
|
62
76
|
|
77
|
+
|
63
78
|
# Get the list of process alias keywords as typically defined by the shell.
|
64
79
|
#
|
65
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
|
66
87
|
|
67
88
|
PS_ALIASES_DEFAULT={
|
68
89
|
'%cpu'=>'pcpu',
|
@@ -106,6 +127,12 @@ module Process
|
|
106
127
|
# Get the list of process alias keywords as typically defined by the shell.
|
107
128
|
#
|
108
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
|
109
136
|
|
110
137
|
def self.ps_aliases
|
111
138
|
@@ps_aliases||=PS_ALIASES_DEFAULT
|
@@ -115,6 +142,13 @@ module Process
|
|
115
142
|
# Set the list of process alias keywords.
|
116
143
|
#
|
117
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
|
118
152
|
|
119
153
|
def self.ps_aliases=(aliases)
|
120
154
|
@@ps_aliases=aliases
|
@@ -128,8 +162,12 @@ module Process
|
|
128
162
|
|
129
163
|
# Get the list of process keywords.
|
130
164
|
#
|
131
|
-
#
|
132
|
-
# Process.ps_keys
|
165
|
+
# @example
|
166
|
+
# Process.ps_keys
|
167
|
+
# => ["blocked","group","pending","size"]
|
168
|
+
#
|
169
|
+
# @return [Array<String>] the list of process keywords.
|
170
|
+
|
133
171
|
|
134
172
|
def self.ps_keys
|
135
173
|
@@ps_keys||=PS_KEYS_DEFAULT
|
@@ -138,8 +176,11 @@ module Process
|
|
138
176
|
|
139
177
|
# Set the list of process keywords.
|
140
178
|
#
|
141
|
-
#
|
179
|
+
# @example
|
142
180
|
# Process.ps_keys = ["blocked","group","pending","size"]
|
181
|
+
#
|
182
|
+
# @param [Array<String>] keywords
|
183
|
+
# @return [Array<String>] keywords
|
143
184
|
|
144
185
|
def self.ps_keys=(keys)
|
145
186
|
@@ps_keys=keys
|
@@ -153,8 +194,11 @@ module Process
|
|
153
194
|
|
154
195
|
# Get the process command, i.e. what the sytem will call for the "ps" command.
|
155
196
|
#
|
156
|
-
#
|
157
|
-
# Process.ps_command
|
197
|
+
# @example
|
198
|
+
# Process.ps_command
|
199
|
+
# => "ps h ww -o blocked,group,pending,size"
|
200
|
+
#
|
201
|
+
# @return [String] the process command
|
158
202
|
|
159
203
|
def self.ps_command
|
160
204
|
@@ps_command||=PS_COMMAND_DEFAULT
|
@@ -163,9 +207,12 @@ module Process
|
|
163
207
|
|
164
208
|
# Set the process command, i.e. what the sytem will call for the "ps" command.
|
165
209
|
#
|
166
|
-
#
|
210
|
+
# @example
|
167
211
|
# Process.ps_command = "ps h ww -o blocked,group,pending,size"
|
168
|
-
|
212
|
+
#
|
213
|
+
# @param [String] the process command
|
214
|
+
# @return [String] the process command
|
215
|
+
|
169
216
|
def self.ps_command=(command)
|
170
217
|
@@ps_command=command
|
171
218
|
end
|
@@ -4,44 +4,56 @@
|
|
4
4
|
|
5
5
|
class String
|
6
6
|
|
7
|
-
ACCENTS = Hash[*'à a á a â a ã a ä a å a ā a ă a æ ae ď d đ d ç c ć c č c ĉ c ċ c è e é e ê e ë e ē e ę e ě e ĕ e ė e ƒ f ĝ g ğ g ġ g ģ g ĥ h ħ h ì i ì i í i î i ï i ī i ĩ i ĭ i į j ı j ij j ĵ j ķ k ĸ k ł l ľ l ĺ l ļ l ŀ l
|
8
|
-
ñ n ń n ň n ņ n ʼn n ŋ n ò o ó o ô o õ o ö o ø o ō o ő o ŏ o ŏ o œ oek ą q ŕ r ř r ŗ r ś s š s ş s ŝ s ș s ť t ţ t ŧ t ț t ù u ú u û u ü u ū u ů u ű u ŭ u ũ u ų u ŵ w ý y ÿ y ŷ y ž z ż z ź z'.split]
|
9
7
|
|
10
|
-
|
11
|
-
#
|
8
|
+
# @return [String] self, with words capitalized
|
9
|
+
# @example
|
10
|
+
# "foo goo hoo".capitalize_words
|
11
|
+
# => "Foo Goo Hoo"
|
12
12
|
|
13
13
|
def capitalize_words
|
14
14
|
split(/\b/).map{|word| word.capitalize }.join
|
15
15
|
end
|
16
16
|
|
17
17
|
|
18
|
-
#
|
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"]
|
19
22
|
|
20
23
|
def words
|
21
24
|
split(/\W*\b\W*/)
|
22
25
|
end
|
23
26
|
|
24
27
|
|
25
|
-
#
|
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"]
|
26
32
|
|
27
33
|
def split_tab
|
28
34
|
split(/\t/)
|
29
35
|
end
|
30
36
|
|
31
37
|
|
32
|
-
# Return an array that is the string split at newlines, then tabs.
|
33
38
|
# This is useful to split a TSV (Tab Separated Values) string
|
34
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"]]
|
35
46
|
|
36
47
|
def split_tsv
|
37
48
|
split(/\n/).map{|line| line.split(/\t/)}
|
38
49
|
end
|
39
50
|
|
40
51
|
|
41
|
-
#
|
42
|
-
#
|
52
|
+
# @return [String] self in lowercase,
|
53
|
+
# with any non-word-characters
|
54
|
+
# replaced with single underscores (aka low dashes).
|
43
55
|
#
|
44
|
-
#
|
56
|
+
# @example
|
45
57
|
# 'Foo Goo Hoo' => 'foo_goo_hoo'
|
46
58
|
# 'Foo***Goo***Hoo' => 'foo_goo_hoo'
|
47
59
|
|
@@ -50,9 +62,9 @@ class String
|
|
50
62
|
end
|
51
63
|
|
52
64
|
|
53
|
-
#
|
65
|
+
# @return [String] the string as an XML id, which is the same as #lowcase
|
54
66
|
#
|
55
|
-
#
|
67
|
+
# @example
|
56
68
|
# "Foo Hoo Goo" => 'foo_goo_hoo'
|
57
69
|
# "Foo***Goo***Hoo" => 'foo_goo_hoo'
|
58
70
|
|
@@ -63,6 +75,8 @@ class String
|
|
63
75
|
# Ruby String#to_class method to convert from a String to a class
|
64
76
|
#
|
65
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
|
66
80
|
|
67
81
|
def to_class
|
68
82
|
split('::').inject(Kernel) {|scope, const_name| scope.const_get(const_name)}
|
@@ -71,12 +85,14 @@ class String
|
|
71
85
|
|
72
86
|
# Increment the rightmost natural number
|
73
87
|
#
|
74
|
-
#
|
88
|
+
# @return [String] the string with an incremented rightmost number
|
89
|
+
#
|
90
|
+
# @example
|
75
91
|
# 'foo5bar'.increment => 'foo4bar'
|
76
92
|
# 'foo5bar'.increment(3) => 'foo8bar'
|
77
93
|
# 'foo9bar'.increment => 'foo10bar'
|
78
94
|
#
|
79
|
-
#
|
95
|
+
# @see String#decrement
|
80
96
|
|
81
97
|
def increment(step=1)
|
82
98
|
self=~/\d+/ ? $`+($&.to_i+step).to_s+$' : self
|
@@ -85,21 +101,23 @@ class String
|
|
85
101
|
|
86
102
|
# Decrement the rightmost natural number
|
87
103
|
#
|
88
|
-
#
|
104
|
+
# @return [String] the string with a decremented rightmost number
|
105
|
+
#
|
106
|
+
# @example
|
89
107
|
# 'foo5bar'.decrement => 'foo4bar'
|
90
108
|
# 'foo5bar'.decrement(3) => 'foo2bar'
|
91
109
|
# 'foo10bar'.derement => 'foo9bar'
|
92
110
|
#
|
93
|
-
#
|
111
|
+
# @see String#increment
|
94
112
|
|
95
113
|
def decrement(step=1)
|
96
114
|
self=~/\d+/ ? $`+($&.to_i-step).to_s+$' : self
|
97
115
|
end
|
98
116
|
|
99
117
|
|
100
|
-
#
|
118
|
+
# @return [String] the previous character, with a changed flag and carry flag
|
101
119
|
#
|
102
|
-
#
|
120
|
+
# @example
|
103
121
|
# String.prev_char('n') => 'm', true, false # change
|
104
122
|
# String.prev_char('a') => 'z', true, true # change & carry
|
105
123
|
# String.prev_char('6') => '5', true, false # change
|
@@ -122,16 +140,16 @@ class String
|
|
122
140
|
end
|
123
141
|
end
|
124
142
|
|
125
|
-
#
|
143
|
+
# @return [String] the previous string
|
126
144
|
#
|
127
|
-
#
|
145
|
+
# @see String#next
|
128
146
|
#
|
129
|
-
#
|
147
|
+
# @example
|
130
148
|
# '888'.prev => '887'
|
131
149
|
# 'n'.prev => 'm'
|
132
150
|
# 'N'.prev => 'M'
|
133
151
|
#
|
134
|
-
#
|
152
|
+
# @example with carry
|
135
153
|
# '880'.prev => '879'
|
136
154
|
# 'nna'.prev => 'nmz'
|
137
155
|
# 'NNA'.prev => 'NMZ'
|
@@ -143,6 +161,8 @@ class String
|
|
143
161
|
|
144
162
|
|
145
163
|
# Do String#prev in place
|
164
|
+
#
|
165
|
+
# @return [String] self
|
146
166
|
|
147
167
|
def prev!
|
148
168
|
return self if length==0
|
@@ -156,6 +176,7 @@ class String
|
|
156
176
|
index-=1
|
157
177
|
return nil if index<0
|
158
178
|
end
|
179
|
+
return self
|
159
180
|
end
|
160
181
|
|
161
182
|
alias pred prev # String#pred : predecessor :: String#succ : successor
|
@@ -177,15 +198,24 @@ class String
|
|
177
198
|
#
|
178
199
|
##
|
179
200
|
|
180
|
-
#
|
201
|
+
# @return [Integer[ a random length suitable for a "lorem ipsum" string.
|
181
202
|
#
|
182
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
|
183
209
|
|
184
210
|
def self.lorem_length
|
185
211
|
1+rand(10)
|
186
212
|
end
|
187
213
|
|
188
|
-
#
|
214
|
+
# @return [String] a random string suitable for "lorem ipsum" text.
|
215
|
+
#
|
216
|
+
# @example
|
217
|
+
# String.lorem => "galkjadscals"
|
218
|
+
# String.lorem(4) => "qtgf"
|
189
219
|
#
|
190
220
|
# This method chooses from lowercase letters a-z.
|
191
221
|
#
|