hash-utils 0.18.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES.txt +13 -0
- data/Gemfile +1 -0
- data/Gemfile.lock +4 -2
- data/README.md +3 -2
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/hash-utils.gemspec +15 -14
- data/lib/hash-utils.rb +1 -0
- data/lib/hash-utils/array.rb +65 -24
- data/lib/hash-utils/boolean.rb +52 -0
- data/lib/hash-utils/file.rb +47 -14
- data/lib/hash-utils/hash.rb +28 -2
- data/lib/hash-utils/nil.rb +22 -0
- data/lib/hash-utils/numeric.rb +2 -9
- data/lib/hash-utils/string.rb +101 -16
- data/lib/hash-utils/stringio.rb +1 -1
- data/test +51 -2
- metadata +65 -51
data/CHANGES.txt
CHANGED
@@ -1,4 +1,17 @@
|
|
1
1
|
|
2
|
+
1.0.0 (2011-10-17)
|
3
|
+
* Boolean: #convert, #to_i
|
4
|
+
* String: #to_boolean
|
5
|
+
* Nil: #to_boolean
|
6
|
+
* Array: #clean, #clean!
|
7
|
+
* Hash: #clean, #clean!
|
8
|
+
* String: #random
|
9
|
+
* Numeric#compare is deprecated
|
10
|
+
* 'ruby-version' dependency
|
11
|
+
* conditional methods definitions depending to Ruby version
|
12
|
+
* StringIO have correct ancestor now
|
13
|
+
* File#write now writes non-binary again, binary writing moved to File#binwrite
|
14
|
+
|
2
15
|
0.18.1 (2011-08-22)
|
3
16
|
* String: #interlace, #interlace!
|
4
17
|
* Object#instance_of_any? and #kind_of_any? receives both class or
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -7,9 +7,10 @@ GEM
|
|
7
7
|
git (>= 1.2.5)
|
8
8
|
rake
|
9
9
|
rake (0.9.2)
|
10
|
-
riot (0.12.
|
10
|
+
riot (0.12.5)
|
11
11
|
rr
|
12
|
-
rr (1.0.
|
12
|
+
rr (1.0.4)
|
13
|
+
ruby-version (0.3.1)
|
13
14
|
|
14
15
|
PLATFORMS
|
15
16
|
ruby
|
@@ -18,3 +19,4 @@ DEPENDENCIES
|
|
18
19
|
bundler (>= 1.0.0)
|
19
20
|
jeweler (>= 1.5.2)
|
20
21
|
riot (>= 0.12.3)
|
22
|
+
ruby-version
|
data/README.md
CHANGED
@@ -7,13 +7,14 @@ to several core classes. It tries to be similar project to
|
|
7
7
|
[Ruby Facets][1] on principle, but less complex, more practical,
|
8
8
|
non-atomic and organized by better way.
|
9
9
|
|
10
|
-
- `Array` –
|
10
|
+
- `Array` – 14 methods,
|
11
11
|
- `FalseClass` – 3 methods,
|
12
12
|
- `File` – 2 methods,
|
13
13
|
- `Gem` – 2 methods,
|
14
|
-
- `Hash` –
|
14
|
+
- `Hash` – 40 methods,
|
15
15
|
- `IO` – 1 method,
|
16
16
|
- `Module` – 1 method,
|
17
|
+
- `NilClass` – 1 method,
|
17
18
|
- `Numeric` – 5 method,
|
18
19
|
- `Object` – 13 methods,
|
19
20
|
- `Proc` – 1 method,
|
data/Rakefile
CHANGED
@@ -20,7 +20,7 @@ Jeweler::Tasks.new do |gem|
|
|
20
20
|
gem.license = "MIT"
|
21
21
|
gem.summary = 'Adds more than 125 useful and frequently rather fundamental methods which are missing in Ruby programming language, to Array, File, Hash, Module, Object, String and Symbol classes. It tries to be similar project to Ruby Facets on principle, but less complex, more practical, non-atomic and organized by better way.'
|
22
22
|
#gem.post_install_message = "\nHASH UTILS: Be warn, Hash#all? has been removed since version 0.15.0 because of\nconflict with built-in one with in fact equivalent functionallity. Please, check\nyour code if you can and switch to Ruby's one.\n\n"
|
23
|
-
gem.post_install_message = "\nHASH UTILS:
|
23
|
+
gem.post_install_message = "\nHASH UTILS: File#write now works non-binary again. Binary writing were moved to File#binwrite.\n\n"
|
24
24
|
gem.email = "martinkozak@martinkozak.net"
|
25
25
|
gem.authors = ["Martin Kozák"]
|
26
26
|
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
1.0.0
|
data/hash-utils.gemspec
CHANGED
@@ -4,13 +4,13 @@
|
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
|
-
s.name =
|
8
|
-
s.version = "0.
|
7
|
+
s.name = "hash-utils"
|
8
|
+
s.version = "1.0.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = [
|
12
|
-
s.date =
|
13
|
-
s.email =
|
11
|
+
s.authors = ["Martin Koz\u{e1}k"]
|
12
|
+
s.date = "2011-10-17"
|
13
|
+
s.email = "martinkozak@martinkozak.net"
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"LICENSE.txt",
|
16
16
|
"README.md"
|
@@ -34,6 +34,7 @@ Gem::Specification.new do |s|
|
|
34
34
|
"lib/hash-utils/hash.rb",
|
35
35
|
"lib/hash-utils/io.rb",
|
36
36
|
"lib/hash-utils/module.rb",
|
37
|
+
"lib/hash-utils/nil.rb",
|
37
38
|
"lib/hash-utils/numeric.rb",
|
38
39
|
"lib/hash-utils/object.rb",
|
39
40
|
"lib/hash-utils/proc.rb",
|
@@ -42,29 +43,29 @@ Gem::Specification.new do |s|
|
|
42
43
|
"lib/hash-utils/symbol.rb",
|
43
44
|
"test"
|
44
45
|
]
|
45
|
-
s.homepage =
|
46
|
-
s.licenses = [
|
47
|
-
s.post_install_message =
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
s.require_paths = [%q{lib}]
|
52
|
-
s.rubygems_version = %q{1.8.8}
|
53
|
-
s.summary = %q{Adds more than 125 useful and frequently rather fundamental methods which are missing in Ruby programming language, to Array, File, Hash, Module, Object, String and Symbol classes. It tries to be similar project to Ruby Facets on principle, but less complex, more practical, non-atomic and organized by better way.}
|
46
|
+
s.homepage = "http://github.com/martinkozak/hash-utils"
|
47
|
+
s.licenses = ["MIT"]
|
48
|
+
s.post_install_message = "\nHASH UTILS: File#write now works non-binary again. Binary writing were moved to File#binwrite.\n\n"
|
49
|
+
s.require_paths = ["lib"]
|
50
|
+
s.rubygems_version = "1.8.11"
|
51
|
+
s.summary = "Adds more than 125 useful and frequently rather fundamental methods which are missing in Ruby programming language, to Array, File, Hash, Module, Object, String and Symbol classes. It tries to be similar project to Ruby Facets on principle, but less complex, more practical, non-atomic and organized by better way."
|
54
52
|
|
55
53
|
if s.respond_to? :specification_version then
|
56
54
|
s.specification_version = 3
|
57
55
|
|
58
56
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
57
|
+
s.add_runtime_dependency(%q<ruby-version>, [">= 0"])
|
59
58
|
s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
|
60
59
|
s.add_development_dependency(%q<jeweler>, [">= 1.5.2"])
|
61
60
|
s.add_development_dependency(%q<riot>, [">= 0.12.3"])
|
62
61
|
else
|
62
|
+
s.add_dependency(%q<ruby-version>, [">= 0"])
|
63
63
|
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
64
64
|
s.add_dependency(%q<jeweler>, [">= 1.5.2"])
|
65
65
|
s.add_dependency(%q<riot>, [">= 0.12.3"])
|
66
66
|
end
|
67
67
|
else
|
68
|
+
s.add_dependency(%q<ruby-version>, [">= 0"])
|
68
69
|
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
69
70
|
s.add_dependency(%q<jeweler>, [">= 1.5.2"])
|
70
71
|
s.add_dependency(%q<riot>, [">= 0.12.3"])
|
data/lib/hash-utils.rb
CHANGED
data/lib/hash-utils/array.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
3
3
|
|
4
|
-
require "
|
4
|
+
require "ruby-version"
|
5
5
|
|
6
6
|
##
|
7
7
|
# Array extension.
|
@@ -174,30 +174,44 @@ class Array
|
|
174
174
|
def eighth
|
175
175
|
self[7]
|
176
176
|
end
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
or RUBY_VERSION[0..4] == "1.8.7" \
|
196
|
-
or (Gem::require_available("ruby-version") and Ruby::Version > [1, 8, 7])
|
197
|
-
|
198
|
-
@__sum_check_done = true
|
177
|
+
|
178
|
+
if Ruby::Version >= [1, 8, 7]
|
179
|
+
|
180
|
+
##
|
181
|
+
# Returns sum of items in the array. If other than numeric type found,
|
182
|
+
# it will be normally conacenated if it's possible.
|
183
|
+
#
|
184
|
+
# @example
|
185
|
+
# array = [1, 2, 3]
|
186
|
+
# array.sum # will return 6
|
187
|
+
# array = ["a", "b", "c"]
|
188
|
+
# array.sum # will return "abc"
|
189
|
+
#
|
190
|
+
# @return [Object] result of summing
|
191
|
+
# @since 0.16.0
|
192
|
+
#
|
193
|
+
|
194
|
+
def sum
|
199
195
|
self.inject(:+)
|
200
|
-
|
196
|
+
end
|
197
|
+
|
198
|
+
else
|
199
|
+
|
200
|
+
##
|
201
|
+
# Returns sum of items in the array. If other than numeric type found,
|
202
|
+
# it will be normally conacenated if it's possible.
|
203
|
+
#
|
204
|
+
# @example
|
205
|
+
# array = [1, 2, 3]
|
206
|
+
# array.sum # will return 6
|
207
|
+
# array = ["a", "b", "c"]
|
208
|
+
# array.sum # will return "abc"
|
209
|
+
#
|
210
|
+
# @return [Object] result of summing
|
211
|
+
# @since 0.16.0
|
212
|
+
#
|
213
|
+
|
214
|
+
def sum
|
201
215
|
first = true
|
202
216
|
self.inject(self.first) do |sum, i|
|
203
217
|
if first
|
@@ -238,5 +252,32 @@ class Array
|
|
238
252
|
def array?
|
239
253
|
true
|
240
254
|
end
|
255
|
+
|
256
|
+
##
|
257
|
+
# Removes given value from the array. It's +nil+ by default,
|
258
|
+
# so behaves just as {Array#compact}.
|
259
|
+
#
|
260
|
+
# @param [Object] value required value; uses +===+
|
261
|
+
# for comparing
|
262
|
+
# @since 0.19.0
|
263
|
+
#
|
264
|
+
|
265
|
+
def clean(value = nil)
|
266
|
+
self.reject! { |v| v === value }
|
267
|
+
end
|
268
|
+
|
269
|
+
##
|
270
|
+
# Removes given value from the array in place. It's +nil+
|
271
|
+
# by default, so behaves just as {Array#compact}.
|
272
|
+
#
|
273
|
+
# @param [Object] value required value; uses +===+
|
274
|
+
# for comparing
|
275
|
+
# @since 0.19.0
|
276
|
+
#
|
277
|
+
|
278
|
+
def clean!(value = nil)
|
279
|
+
self.reject! { |v| v === value }
|
280
|
+
end
|
241
281
|
|
242
282
|
end
|
283
|
+
|
data/lib/hash-utils/boolean.rb
CHANGED
@@ -41,6 +41,32 @@ class TrueClass
|
|
41
41
|
true
|
42
42
|
end
|
43
43
|
|
44
|
+
##
|
45
|
+
# Converts value to another type.
|
46
|
+
#
|
47
|
+
# @param [Object] t true value equivalent
|
48
|
+
# @param [Object] f false value equivalent
|
49
|
+
# @return [Object] appropriate response
|
50
|
+
# @since 0.19.0
|
51
|
+
#
|
52
|
+
|
53
|
+
def convert(t, f)
|
54
|
+
t
|
55
|
+
end
|
56
|
+
|
57
|
+
##
|
58
|
+
# Converts value to integer.
|
59
|
+
#
|
60
|
+
# @param [Object] t true value equivalent
|
61
|
+
# @param [Object] f false value equivalent
|
62
|
+
# @return [Object] appropriate equivalent
|
63
|
+
# @since 0.19.0
|
64
|
+
#
|
65
|
+
|
66
|
+
def to_i(t = 1, f = 0)
|
67
|
+
self.convert(t, f)
|
68
|
+
end
|
69
|
+
|
44
70
|
end
|
45
71
|
|
46
72
|
##
|
@@ -83,4 +109,30 @@ class FalseClass
|
|
83
109
|
false
|
84
110
|
end
|
85
111
|
|
112
|
+
##
|
113
|
+
# Converts value to another type.
|
114
|
+
#
|
115
|
+
# @param [Object] t true value equivalent
|
116
|
+
# @param [Object] f false value equivalent
|
117
|
+
# @return [Object] appropriate equivalent
|
118
|
+
# @since 0.19.0
|
119
|
+
#
|
120
|
+
|
121
|
+
def convert(t, f)
|
122
|
+
f
|
123
|
+
end
|
124
|
+
|
125
|
+
##
|
126
|
+
# Converts value to integer.
|
127
|
+
#
|
128
|
+
# @param [Object] t true value equivalent
|
129
|
+
# @param [Object] f false value equivalent
|
130
|
+
# @return [Object] appropriate equivalent
|
131
|
+
# @since 0.19.0
|
132
|
+
#
|
133
|
+
|
134
|
+
def to_i(t = 1, f = 0)
|
135
|
+
self.convert(t, f)
|
136
|
+
end
|
137
|
+
|
86
138
|
end
|
data/lib/hash-utils/file.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
3
3
|
|
4
|
+
require "ruby-version"
|
5
|
+
|
4
6
|
##
|
5
7
|
# File extension.
|
6
8
|
# @since 0.11.0
|
@@ -8,22 +10,53 @@
|
|
8
10
|
|
9
11
|
class File
|
10
12
|
|
11
|
-
##
|
12
|
-
# Writes data to file and closes it in single call. Data are written
|
13
|
-
# in binary mode since +0.11.1+.
|
14
|
-
#
|
15
|
-
# @param [String] filepath path to file
|
16
|
-
# @param [String] data data for write
|
17
|
-
# @return [Integer] length of really written data
|
18
|
-
# @since 0.11.0
|
19
|
-
#
|
20
13
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
14
|
+
if Ruby::Version < [1, 9, 3]
|
15
|
+
|
16
|
+
##
|
17
|
+
# Writes data to file and closes it in single call.
|
18
|
+
#
|
19
|
+
# @note Data were written in binary mode since +0.11.1+,
|
20
|
+
# but since +0.19.0+, they are written non-binary again.
|
21
|
+
# Binary writing is implemented via {File#binwrite}.
|
22
|
+
# @note Since Ruby 1.9.3 replaced by STL native version.
|
23
|
+
#
|
24
|
+
# @param [String] filepath path to file
|
25
|
+
# @param [String] data data for write
|
26
|
+
# @return [Integer] length of really written data
|
27
|
+
# @since 0.11.0
|
28
|
+
#
|
29
|
+
|
30
|
+
def self.write(filepath, data = "")
|
31
|
+
len = nil
|
32
|
+
File.open(filepath, "w") do |io|
|
33
|
+
len = io.write(data)
|
34
|
+
end
|
35
|
+
return len
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
if Ruby::Version < [1, 9, 3]
|
40
|
+
|
41
|
+
##
|
42
|
+
# Writes binary data to file and closes it
|
43
|
+
# in single call.
|
44
|
+
#
|
45
|
+
# @note Since Ruby 1.9.3 replaced by STL native version.
|
46
|
+
#
|
47
|
+
# @param [String] filepath path to file
|
48
|
+
# @param [String] data data for write
|
49
|
+
# @return [Integer] length of really written data
|
50
|
+
# @since 0.19.0
|
51
|
+
#
|
52
|
+
|
53
|
+
def self.binwrite(filepath, data = "")
|
54
|
+
len = nil
|
55
|
+
File.open(filepath, "wb") do |io|
|
56
|
+
len = io.write(data)
|
57
|
+
end
|
58
|
+
return len
|
25
59
|
end
|
26
|
-
return len
|
27
60
|
end
|
28
61
|
|
29
62
|
##
|
data/lib/hash-utils/hash.rb
CHANGED
@@ -104,7 +104,7 @@ class Hash
|
|
104
104
|
#
|
105
105
|
|
106
106
|
def compact
|
107
|
-
self.
|
107
|
+
self.clean()
|
108
108
|
end
|
109
109
|
|
110
110
|
##
|
@@ -116,9 +116,35 @@ class Hash
|
|
116
116
|
#
|
117
117
|
|
118
118
|
def compact!
|
119
|
-
self.
|
119
|
+
self.clean!
|
120
120
|
end
|
121
121
|
|
122
|
+
##
|
123
|
+
# Removes given value from the hash. It's +nil+ by default,
|
124
|
+
# so behaves just as {#compact}.
|
125
|
+
#
|
126
|
+
# @param [Object] value required value; uses +===+
|
127
|
+
# for comparing
|
128
|
+
# @since 0.19.0
|
129
|
+
#
|
130
|
+
|
131
|
+
def clean(value = nil)
|
132
|
+
self.reject { |k, v| v === value }
|
133
|
+
end
|
134
|
+
|
135
|
+
##
|
136
|
+
# Removes given value from the hash in place. It's +nil+
|
137
|
+
# by default, so behaves just as {#compact}.
|
138
|
+
#
|
139
|
+
# @param [Object] value required value; uses +===+
|
140
|
+
# for comparing
|
141
|
+
# @since 0.19.0
|
142
|
+
#
|
143
|
+
|
144
|
+
def clean!(value = nil)
|
145
|
+
self.reject! { |k, v| v === value }
|
146
|
+
end
|
147
|
+
|
122
148
|
##
|
123
149
|
# Returns a new hash with the results of running block once for
|
124
150
|
# every pair in +self+.
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
3
|
+
|
4
|
+
##
|
5
|
+
# NilClass extension.
|
6
|
+
# @since 0.19.0
|
7
|
+
#
|
8
|
+
|
9
|
+
class NilClass
|
10
|
+
|
11
|
+
##
|
12
|
+
# Converts +nil+ to boolean.
|
13
|
+
#
|
14
|
+
# @return [FalseClass] result
|
15
|
+
# @since 0.19.0
|
16
|
+
#
|
17
|
+
|
18
|
+
def to_boolean(*args)
|
19
|
+
false
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
data/lib/hash-utils/numeric.rb
CHANGED
@@ -16,19 +16,12 @@ class Numeric
|
|
16
16
|
#
|
17
17
|
# @param [Numeric] number number for comparsion
|
18
18
|
# @return [Integer] comparsion result, see above
|
19
|
+
# @deprecated (since 0.19.0) Use the +#<=>+ operator instead.
|
19
20
|
# @since 0.16.0
|
20
21
|
#
|
21
22
|
|
22
23
|
def compare(number)
|
23
|
-
|
24
|
-
|
25
|
-
if comp.zero?
|
26
|
-
0
|
27
|
-
elsif comp.positive?
|
28
|
-
1
|
29
|
-
else
|
30
|
-
-1
|
31
|
-
end
|
24
|
+
self <=> number
|
32
25
|
end
|
33
26
|
|
34
27
|
##
|
data/lib/hash-utils/string.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
3
3
|
|
4
|
+
require "ruby-version"
|
4
5
|
require "hash-utils/array"
|
5
6
|
require "hash-utils/hash"
|
6
7
|
require "hash-utils/object"
|
@@ -13,16 +14,79 @@ class String
|
|
13
14
|
|
14
15
|
##
|
15
16
|
# Holds numeric matcher.
|
17
|
+
# @since 0.3.0
|
16
18
|
#
|
17
19
|
|
18
20
|
NUMERIC = /^\s*-?\d+(?:\.\d+)?\s*$/
|
19
21
|
|
20
22
|
##
|
21
23
|
# Holds character interlacing matcher.
|
24
|
+
# @since 0.18.1
|
22
25
|
#
|
23
26
|
|
24
27
|
INTERLACING = /(.)([^$])/
|
25
28
|
|
29
|
+
##
|
30
|
+
# Holds lower case alpha characters for random string generator.
|
31
|
+
# @since 0.19.0
|
32
|
+
#
|
33
|
+
|
34
|
+
RANDOM_ALPHA_LOWER = "abcdefghijklmnopqrstuvwxyz"
|
35
|
+
|
36
|
+
##
|
37
|
+
# Holds upper case alpha characters for random string generator.
|
38
|
+
# @since 0.19.0
|
39
|
+
#
|
40
|
+
|
41
|
+
RANDOM_ALPHA_UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
42
|
+
|
43
|
+
##
|
44
|
+
# Holds number characters list for random string generator.
|
45
|
+
# @since 0.19.0
|
46
|
+
#
|
47
|
+
|
48
|
+
RANDOM_NUMBERS = "0123456789"
|
49
|
+
|
50
|
+
##
|
51
|
+
# Holds symbols list for random string generator.
|
52
|
+
# @since 0.19.0
|
53
|
+
#
|
54
|
+
|
55
|
+
RANDOM_SYMBOLS = "!\/#?.,_-+*@%&{}[]()=<>|~'$"
|
56
|
+
|
57
|
+
##
|
58
|
+
# Holds whitespace for random string generator.
|
59
|
+
# @since 0.19.0
|
60
|
+
#
|
61
|
+
|
62
|
+
RANDOM_WHITESPACE = " "
|
63
|
+
|
64
|
+
##
|
65
|
+
# Holds full spectrum of possible characters in random string generator.
|
66
|
+
# @since 0.19.0
|
67
|
+
#
|
68
|
+
|
69
|
+
RANDOM_FULL = self::RANDOM_ALPHA_LOWER + self::RANDOM_ALPHA_UPPER + self::RANDOM_NUMBERS + self::RANDOM_SYMBOLS + self::RANDOM_WHITESPACE
|
70
|
+
|
71
|
+
##
|
72
|
+
# Returns random string.
|
73
|
+
#
|
74
|
+
# @param [Integer] length length of the required string
|
75
|
+
# @param [String] characters list of the characters
|
76
|
+
# @return [String] new random string
|
77
|
+
# @since 0.19.0
|
78
|
+
#
|
79
|
+
|
80
|
+
def self.random(length, characters = self::RANDOM_FULL)
|
81
|
+
result = ""
|
82
|
+
max = characters.length
|
83
|
+
length.times do
|
84
|
+
result << characters[Kernel.rand(max)].chr
|
85
|
+
end
|
86
|
+
|
87
|
+
return result
|
88
|
+
end
|
89
|
+
|
26
90
|
##
|
27
91
|
# Indicates, string is numeric, so consists of numbers only.
|
28
92
|
#
|
@@ -512,19 +576,27 @@ class String
|
|
512
576
|
return res
|
513
577
|
end
|
514
578
|
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
579
|
+
if Ruby::Version < [1, 9, 3]
|
580
|
+
|
581
|
+
##
|
582
|
+
# Puts content to begin of string.
|
583
|
+
#
|
584
|
+
# @note Since Ruby 1.9.3 replaced by STL native version.
|
585
|
+
#
|
586
|
+
# @param [String] string string for prepend
|
587
|
+
# @return [String] itself
|
588
|
+
# @since 0.12.0
|
589
|
+
#
|
590
|
+
|
591
|
+
def unshift(string)
|
592
|
+
self.replace(string + self)
|
593
|
+
end
|
594
|
+
|
595
|
+
alias :prepend :unshift
|
596
|
+
else
|
597
|
+
alias :unshift :prepend
|
598
|
+
end
|
526
599
|
|
527
|
-
alias :prepend :unshift
|
528
600
|
alias :append :<<
|
529
601
|
|
530
602
|
##
|
@@ -627,10 +699,9 @@ class String
|
|
627
699
|
#
|
628
700
|
# @param [String] string string which will interlaced to
|
629
701
|
# @return [String] new interlaced string
|
630
|
-
# @since 0.
|
702
|
+
# @since 0.18.1
|
631
703
|
#
|
632
704
|
|
633
|
-
public
|
634
705
|
def interlace(string)
|
635
706
|
self.gsub(self.class::INTERLACING, '\1' << string << '\2' << string)
|
636
707
|
end
|
@@ -641,15 +712,29 @@ class String
|
|
641
712
|
# @param [String] string string which will interlaced to
|
642
713
|
# @return [String] itself
|
643
714
|
# @see #interlace
|
644
|
-
# @since 0.
|
715
|
+
# @since 0.18.1
|
645
716
|
#
|
646
717
|
|
647
|
-
public
|
648
718
|
def interlace!(string)
|
649
719
|
self.gsub!(self.class::INTERLACING, '\1' << string << '\2' << string)
|
650
720
|
self
|
651
721
|
end
|
652
722
|
|
723
|
+
##
|
724
|
+
# Converts string to boolean. Note, it works differently than
|
725
|
+
# {Object#to_b} because it converts to boolean by comparing in
|
726
|
+
# contrast to +#to_b+ which converts by regular Ruby object boolean
|
727
|
+
# evaluation.
|
728
|
+
#
|
729
|
+
# @param [Object] t true equivalent
|
730
|
+
# @return [Boolean] result of conversion
|
731
|
+
# @since 0.19.0
|
732
|
+
#
|
733
|
+
|
734
|
+
def to_boolean(t = "true")
|
735
|
+
self == t
|
736
|
+
end
|
737
|
+
|
653
738
|
|
654
739
|
private
|
655
740
|
|
data/lib/hash-utils/stringio.rb
CHANGED
data/test
CHANGED
@@ -39,11 +39,21 @@ context "Array" do
|
|
39
39
|
asserts("#eighth") do
|
40
40
|
[1, 2, 3, 4, 5, 6, 7, 8].eighth == 8
|
41
41
|
end
|
42
|
+
asserts("#array?") do
|
43
|
+
[1, 2, 3].array?
|
44
|
+
end
|
42
45
|
asserts("#avg") do
|
43
46
|
[1, 2, 3].avg == 2
|
44
47
|
end
|
45
|
-
asserts("#
|
46
|
-
[1,
|
48
|
+
asserts("#clean") do
|
49
|
+
t = [1, nil]
|
50
|
+
t = t.clean(1)
|
51
|
+
t == [nil]
|
52
|
+
end
|
53
|
+
asserts("#clean!") do
|
54
|
+
t = [1, nil]
|
55
|
+
t.clean(1)
|
56
|
+
t == [nil]
|
47
57
|
end
|
48
58
|
asserts("#sum") do
|
49
59
|
[1, 2, 3].sum == 6
|
@@ -56,9 +66,15 @@ context "FalseClass" do
|
|
56
66
|
asserts("#boolean?") do
|
57
67
|
false.boolean?
|
58
68
|
end
|
69
|
+
asserts("#convert") do
|
70
|
+
false.convert(:a, :b) == :b
|
71
|
+
end
|
59
72
|
asserts("#false?") do
|
60
73
|
false.false?
|
61
74
|
end
|
75
|
+
asserts("#to_i") do
|
76
|
+
false.to_i == 0
|
77
|
+
end
|
62
78
|
asserts("#true?") do
|
63
79
|
not false.true?
|
64
80
|
end
|
@@ -111,6 +127,16 @@ context "Hash" do
|
|
111
127
|
asserts("#avg") do
|
112
128
|
topic.avg == 1.5
|
113
129
|
end
|
130
|
+
asserts("#clean") do
|
131
|
+
t = { :a => 1, :b => nil }
|
132
|
+
t = t.clean(1)
|
133
|
+
t == { :b => nil }
|
134
|
+
end
|
135
|
+
asserts("#clean!") do
|
136
|
+
t = { :a => 1, :b => nil }
|
137
|
+
t.clean!(1)
|
138
|
+
t == { :b => nil }
|
139
|
+
end
|
114
140
|
asserts("#compact") do
|
115
141
|
t = { :a => 1, :b => nil }
|
116
142
|
t = t.compact
|
@@ -209,6 +235,14 @@ context "Module" do
|
|
209
235
|
end
|
210
236
|
end
|
211
237
|
|
238
|
+
## NILCLASS
|
239
|
+
|
240
|
+
context "NilClass" do
|
241
|
+
asserts("#to_boolean") do
|
242
|
+
nil.to_boolean("xyz") === false
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
212
246
|
## NUMERIC
|
213
247
|
|
214
248
|
context "Numeric" do
|
@@ -392,6 +426,12 @@ context "String" do
|
|
392
426
|
res &= str.push_lines("1", "2") == "a\nb\nc\nd\n1\n2"
|
393
427
|
res &= str == "a\nb\nc\nd\n1\n2"
|
394
428
|
end
|
429
|
+
asserts("#random") do
|
430
|
+
str1 = String::random(30)
|
431
|
+
str2 = String::random(30)
|
432
|
+
|
433
|
+
str1 != str2
|
434
|
+
end
|
395
435
|
asserts("#shift") do
|
396
436
|
res = true
|
397
437
|
str = "abcd"
|
@@ -422,6 +462,9 @@ context "String" do
|
|
422
462
|
foo.swap_with(bar)
|
423
463
|
foo = "123" and bar = "abc"
|
424
464
|
end
|
465
|
+
asserts("#to_boolean") do
|
466
|
+
"alfa".to_boolean("alfa") == true
|
467
|
+
end
|
425
468
|
asserts("#ucfirst") do
|
426
469
|
str = "abcd"
|
427
470
|
str.ucfirst == "Abcd" and str == "abcd"
|
@@ -497,9 +540,15 @@ context "TrueClass" do
|
|
497
540
|
asserts("#boolean?") do
|
498
541
|
true.boolean?
|
499
542
|
end
|
543
|
+
asserts("#convert") do
|
544
|
+
true.convert(:a, :b) == :a
|
545
|
+
end
|
500
546
|
asserts("#false?") do
|
501
547
|
not true.false?
|
502
548
|
end
|
549
|
+
asserts("#to_i") do
|
550
|
+
true.to_i == 1
|
551
|
+
end
|
503
552
|
asserts("#true?") do
|
504
553
|
true.true?
|
505
554
|
end
|
metadata
CHANGED
@@ -1,60 +1,68 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash-utils
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
4
5
|
prerelease:
|
5
|
-
version: 0.18.1
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
8
|
-
-
|
7
|
+
authors:
|
8
|
+
- Martin Kozák
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
date: 2011-10-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: ruby-version
|
16
|
+
requirement: &19826500 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *19826500
|
25
|
+
- !ruby/object:Gem::Dependency
|
16
26
|
name: bundler
|
17
|
-
requirement: &
|
27
|
+
requirement: &19825240 !ruby/object:Gem::Requirement
|
18
28
|
none: false
|
19
|
-
requirements:
|
20
|
-
- -
|
21
|
-
- !ruby/object:Gem::Version
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
22
32
|
version: 1.0.0
|
23
33
|
type: :development
|
24
34
|
prerelease: false
|
25
|
-
version_requirements: *
|
26
|
-
- !ruby/object:Gem::Dependency
|
35
|
+
version_requirements: *19825240
|
36
|
+
- !ruby/object:Gem::Dependency
|
27
37
|
name: jeweler
|
28
|
-
requirement: &
|
38
|
+
requirement: &19823780 !ruby/object:Gem::Requirement
|
29
39
|
none: false
|
30
|
-
requirements:
|
31
|
-
- -
|
32
|
-
- !ruby/object:Gem::Version
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
33
43
|
version: 1.5.2
|
34
44
|
type: :development
|
35
45
|
prerelease: false
|
36
|
-
version_requirements: *
|
37
|
-
- !ruby/object:Gem::Dependency
|
46
|
+
version_requirements: *19823780
|
47
|
+
- !ruby/object:Gem::Dependency
|
38
48
|
name: riot
|
39
|
-
requirement: &
|
49
|
+
requirement: &19820080 !ruby/object:Gem::Requirement
|
40
50
|
none: false
|
41
|
-
requirements:
|
42
|
-
- -
|
43
|
-
- !ruby/object:Gem::Version
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
44
54
|
version: 0.12.3
|
45
55
|
type: :development
|
46
56
|
prerelease: false
|
47
|
-
version_requirements: *
|
57
|
+
version_requirements: *19820080
|
48
58
|
description:
|
49
59
|
email: martinkozak@martinkozak.net
|
50
60
|
executables: []
|
51
|
-
|
52
61
|
extensions: []
|
53
|
-
|
54
|
-
extra_rdoc_files:
|
62
|
+
extra_rdoc_files:
|
55
63
|
- LICENSE.txt
|
56
64
|
- README.md
|
57
|
-
files:
|
65
|
+
files:
|
58
66
|
- .document
|
59
67
|
- CHANGES.txt
|
60
68
|
- Gemfile
|
@@ -73,6 +81,7 @@ files:
|
|
73
81
|
- lib/hash-utils/hash.rb
|
74
82
|
- lib/hash-utils/io.rb
|
75
83
|
- lib/hash-utils/module.rb
|
84
|
+
- lib/hash-utils/nil.rb
|
76
85
|
- lib/hash-utils/numeric.rb
|
77
86
|
- lib/hash-utils/object.rb
|
78
87
|
- lib/hash-utils/proc.rb
|
@@ -81,35 +90,40 @@ files:
|
|
81
90
|
- lib/hash-utils/symbol.rb
|
82
91
|
- test
|
83
92
|
homepage: http://github.com/martinkozak/hash-utils
|
84
|
-
licenses:
|
93
|
+
licenses:
|
85
94
|
- MIT
|
86
|
-
post_install_message:
|
87
|
-
HASH UTILS: Some methods are optimized at Ruby 1.8.7 and 1.9 or higher, but this feature requires the 'ruby-version' gem.\n\n"
|
88
|
-
rdoc_options: []
|
95
|
+
post_install_message: ! '
|
89
96
|
|
90
|
-
|
97
|
+
HASH UTILS: File#write now works non-binary again. Binary writing were moved to
|
98
|
+
File#binwrite.
|
99
|
+
|
100
|
+
|
101
|
+
'
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
91
104
|
- lib
|
92
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
106
|
none: false
|
94
|
-
requirements:
|
95
|
-
- -
|
96
|
-
- !ruby/object:Gem::Version
|
97
|
-
|
98
|
-
segments:
|
107
|
+
requirements:
|
108
|
+
- - ! '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
segments:
|
99
112
|
- 0
|
100
|
-
|
101
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
hash: 2411575543871344535
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
115
|
none: false
|
103
|
-
requirements:
|
104
|
-
- -
|
105
|
-
- !ruby/object:Gem::Version
|
106
|
-
version:
|
116
|
+
requirements:
|
117
|
+
- - ! '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
107
120
|
requirements: []
|
108
|
-
|
109
121
|
rubyforge_project:
|
110
|
-
rubygems_version: 1.8.
|
122
|
+
rubygems_version: 1.8.11
|
111
123
|
signing_key:
|
112
124
|
specification_version: 3
|
113
|
-
summary: Adds more than 125 useful and frequently rather fundamental methods which
|
125
|
+
summary: Adds more than 125 useful and frequently rather fundamental methods which
|
126
|
+
are missing in Ruby programming language, to Array, File, Hash, Module, Object,
|
127
|
+
String and Symbol classes. It tries to be similar project to Ruby Facets on principle,
|
128
|
+
but less complex, more practical, non-atomic and organized by better way.
|
114
129
|
test_files: []
|
115
|
-
|