useful_class_extensions 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/useful_class_extensions.rb +247 -0
- metadata +2 -7
- data/array.rb +0 -83
- data/fixnum.rb +0 -41
- data/hash.rb +0 -33
- data/nil_class.rb +0 -9
- data/string.rb +0 -68
- data/time.rb +0 -9
@@ -0,0 +1,247 @@
|
|
1
|
+
class Array
|
2
|
+
def sum
|
3
|
+
self.compact.inject(0) { |s,v| s += v }
|
4
|
+
end
|
5
|
+
|
6
|
+
def to_i
|
7
|
+
self.collect{|x| x.to_i}
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_f
|
11
|
+
self.collect{|x| x.to_i}
|
12
|
+
end
|
13
|
+
|
14
|
+
def frequencies
|
15
|
+
new_val = {}
|
16
|
+
self.each do |s|
|
17
|
+
elem = s.to_s
|
18
|
+
new_val[elem].nil? ? new_val[elem]=1 : new_val[elem]+=1
|
19
|
+
end
|
20
|
+
return new_val
|
21
|
+
end
|
22
|
+
|
23
|
+
def chunk(pieces=2)
|
24
|
+
len = self.length
|
25
|
+
return [] if len == 0
|
26
|
+
mid = (len/pieces)
|
27
|
+
chunks = []
|
28
|
+
start = 0
|
29
|
+
1.upto(pieces) do |i|
|
30
|
+
last = start+mid
|
31
|
+
last = last-1 unless len%pieces >= i
|
32
|
+
chunks << self[start..last] || []
|
33
|
+
start = last+1
|
34
|
+
end
|
35
|
+
chunks
|
36
|
+
end
|
37
|
+
|
38
|
+
def repack
|
39
|
+
set = []
|
40
|
+
self.each do |slice|
|
41
|
+
set<<slice
|
42
|
+
yield set
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def centroid
|
47
|
+
dimensions = self.flatten
|
48
|
+
x_cent = (x_vals = 1.upto(dimensions.length).collect{|x| dimensions[x] if x.even?}.compact).sum/x_vals.length
|
49
|
+
y_cent = (y_vals = 1.upto(dimensions.length).collect{|y| dimensions[y] if !y.even?}.compact).sum/y_vals.length
|
50
|
+
return x_cent, y_cent
|
51
|
+
end
|
52
|
+
|
53
|
+
def area
|
54
|
+
side_one = (self[0].to_f-self[2].to_f).abs
|
55
|
+
side_two = (self[1].to_f-self[3].to_f).abs
|
56
|
+
return side_one*side_two
|
57
|
+
end
|
58
|
+
|
59
|
+
def all_combinations(length_range=1..self.length)
|
60
|
+
permutations = []
|
61
|
+
length_range.max.downto(length_range.min) do |length|
|
62
|
+
self.permutation(length).each do |perm|
|
63
|
+
permutations << perm.sort if !permutations.include?(perm.sort)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
return permutations
|
67
|
+
end
|
68
|
+
|
69
|
+
def structs_to_hashes
|
70
|
+
keys = (self.first.methods-Class.methods).collect{|x| x.to_s.gsub("=", "") if x.to_s.include?("=") && x.to_s!= "[]="}.compact
|
71
|
+
hashed_set = []
|
72
|
+
self.each do |struct|
|
73
|
+
object = {}
|
74
|
+
keys.collect{|k| object[k] = k.class == DateTime ? struct.send(k).to_time : struct.send(k)}
|
75
|
+
hashed_set << object
|
76
|
+
end
|
77
|
+
return hashed_set
|
78
|
+
end
|
79
|
+
|
80
|
+
def sth
|
81
|
+
structs_to_hashes
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
class Fixnum
|
86
|
+
|
87
|
+
def days
|
88
|
+
return self*60*60*24
|
89
|
+
end
|
90
|
+
|
91
|
+
def day
|
92
|
+
return days
|
93
|
+
end
|
94
|
+
|
95
|
+
def weeks
|
96
|
+
return self*60*60*24*7
|
97
|
+
end
|
98
|
+
|
99
|
+
def week
|
100
|
+
return weeks
|
101
|
+
end
|
102
|
+
|
103
|
+
def generalized_time_factor
|
104
|
+
if self < 60
|
105
|
+
#one second
|
106
|
+
return 1
|
107
|
+
elsif self < 3600
|
108
|
+
#one minute
|
109
|
+
return 60
|
110
|
+
elsif self < 86400
|
111
|
+
#one hour
|
112
|
+
return 3600
|
113
|
+
elsif self < 604800
|
114
|
+
#one day
|
115
|
+
return 86400
|
116
|
+
elsif self < 11536000
|
117
|
+
#one week
|
118
|
+
return 604800
|
119
|
+
else
|
120
|
+
#four weeks
|
121
|
+
return 2419200
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
class Time
|
128
|
+
def self.ntp
|
129
|
+
return self.at(self.now.to_f)
|
130
|
+
end
|
131
|
+
|
132
|
+
def gmt
|
133
|
+
return to_time.gmtime
|
134
|
+
end
|
135
|
+
end
|
136
|
+
class String
|
137
|
+
|
138
|
+
require 'rubygems'
|
139
|
+
require 'htmlentities'
|
140
|
+
|
141
|
+
def write(str)
|
142
|
+
self << str
|
143
|
+
end
|
144
|
+
|
145
|
+
def underscore
|
146
|
+
self.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').tr("-", "_").downcase
|
147
|
+
end
|
148
|
+
|
149
|
+
def pluralize
|
150
|
+
self.underscore.concat("s")
|
151
|
+
end
|
152
|
+
|
153
|
+
def sanitize_for_streaming
|
154
|
+
# return self.split("").reject {|c| c.match(/[\w\'\-]/).nil?}.to_s
|
155
|
+
return self.gsub(/[\'\"]/, '').gsub("#", "%23").gsub(' ', '%20')
|
156
|
+
end
|
157
|
+
|
158
|
+
def classify
|
159
|
+
if self.split(//).last == "s"
|
160
|
+
if self.split(//)[self.split(//).length-3..self.split(//).length].join == "ies"
|
161
|
+
camelize(self.split(//)[0..self.split(//).length-4].join("")+"y")
|
162
|
+
else
|
163
|
+
camelize(self.sub(/.*\./, '').chop)
|
164
|
+
end
|
165
|
+
else
|
166
|
+
camelize(self.sub(/.*\./, ''))
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
|
171
|
+
if first_letter_in_uppercase
|
172
|
+
lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
def constantize
|
177
|
+
return Object.const_defined?(self) ? Object.const_get(self) : Object.const_missing(self)
|
178
|
+
end
|
179
|
+
|
180
|
+
def to_class
|
181
|
+
return self.classify.constantize
|
182
|
+
end
|
183
|
+
|
184
|
+
def super_strip
|
185
|
+
#This regexp is used in place of \W to allow for # and @ signs.
|
186
|
+
if self.include?("#") || self.include?("@")
|
187
|
+
return self
|
188
|
+
elsif self.include?("http")
|
189
|
+
return self
|
190
|
+
else
|
191
|
+
return self.strip.downcase.gsub(/[!$%\*&:.\;{}\[\]\(\)\-\_+=\'\"\|<>,\/?~`]/, "")
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
def super_split(split_char)
|
196
|
+
#This regexp is used in place of \W to allow for # and @ signs.
|
197
|
+
return self.gsub(/[!$%\*\;{}\[\]\(\)\+=\'\"\|<>,~`]/, " ").split(split_char)
|
198
|
+
end
|
199
|
+
|
200
|
+
def blank?
|
201
|
+
return self.empty? || self.nil?
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
class NilClass
|
206
|
+
def empty?
|
207
|
+
return true
|
208
|
+
end
|
209
|
+
|
210
|
+
def blank?
|
211
|
+
return true
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
class Hash
|
216
|
+
def flat_each(prefix=[], &blk)
|
217
|
+
each do |k,v|
|
218
|
+
if v.is_a?(Hash)
|
219
|
+
v.flat_each(prefix+[k], &blk)
|
220
|
+
else
|
221
|
+
yield prefix+[k], v
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
def flatify
|
227
|
+
hh = {}
|
228
|
+
self.to_enum(:flat_each).collect { |k,v| [k.join("-"),v] }.collect {|attrib| hh[attrib[0]] = attrib[1]}
|
229
|
+
return hh
|
230
|
+
end
|
231
|
+
|
232
|
+
def highest
|
233
|
+
high_pair = self.max {|a,b| a[1] <=> b[1]}
|
234
|
+
return {high_pair[0] => high_pair[1]}
|
235
|
+
end
|
236
|
+
|
237
|
+
def lowest
|
238
|
+
low_pair = self.min {|a,b| a[1] <=> b[1]}
|
239
|
+
return {low_pair[0] => low_pair[1]}
|
240
|
+
end
|
241
|
+
|
242
|
+
def self.zip(keys, values, default=nil, &block)
|
243
|
+
hsh = block_given? ? Hash.new(&block) : Hash.new(default)
|
244
|
+
keys.zip(values) { |k,v| hsh[k]=v }
|
245
|
+
hsh
|
246
|
+
end
|
247
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: useful_class_extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -18,12 +18,7 @@ executables: []
|
|
18
18
|
extensions: []
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
|
-
-
|
22
|
-
- fixnum.rb
|
23
|
-
- hash.rb
|
24
|
-
- nil_class.rb
|
25
|
-
- string.rb
|
26
|
-
- time.rb
|
21
|
+
- lib/useful_class_extensions.rb
|
27
22
|
homepage: http://devingaffney.com
|
28
23
|
licenses: []
|
29
24
|
post_install_message:
|
data/array.rb
DELETED
@@ -1,83 +0,0 @@
|
|
1
|
-
class Array
|
2
|
-
def sum
|
3
|
-
self.compact.inject(0) { |s,v| s += v }
|
4
|
-
end
|
5
|
-
|
6
|
-
def to_i
|
7
|
-
self.collect{|x| x.to_i}
|
8
|
-
end
|
9
|
-
|
10
|
-
def to_f
|
11
|
-
self.collect{|x| x.to_i}
|
12
|
-
end
|
13
|
-
|
14
|
-
def frequencies
|
15
|
-
new_val = {}
|
16
|
-
self.each do |s|
|
17
|
-
elem = s.to_s
|
18
|
-
new_val[elem].nil? ? new_val[elem]=1 : new_val[elem]+=1
|
19
|
-
end
|
20
|
-
return new_val
|
21
|
-
end
|
22
|
-
|
23
|
-
def chunk(pieces=2)
|
24
|
-
len = self.length
|
25
|
-
return [] if len == 0
|
26
|
-
mid = (len/pieces)
|
27
|
-
chunks = []
|
28
|
-
start = 0
|
29
|
-
1.upto(pieces) do |i|
|
30
|
-
last = start+mid
|
31
|
-
last = last-1 unless len%pieces >= i
|
32
|
-
chunks << self[start..last] || []
|
33
|
-
start = last+1
|
34
|
-
end
|
35
|
-
chunks
|
36
|
-
end
|
37
|
-
|
38
|
-
def repack
|
39
|
-
set = []
|
40
|
-
self.each do |slice|
|
41
|
-
set<<slice
|
42
|
-
yield set
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def centroid
|
47
|
-
dimensions = self.flatten
|
48
|
-
x_cent = (x_vals = 1.upto(dimensions.length).collect{|x| dimensions[x] if x.even?}.compact).sum/x_vals.length
|
49
|
-
y_cent = (y_vals = 1.upto(dimensions.length).collect{|y| dimensions[y] if !y.even?}.compact).sum/y_vals.length
|
50
|
-
return x_cent, y_cent
|
51
|
-
end
|
52
|
-
|
53
|
-
def area
|
54
|
-
side_one = (self[0].to_f-self[2].to_f).abs
|
55
|
-
side_two = (self[1].to_f-self[3].to_f).abs
|
56
|
-
return side_one*side_two
|
57
|
-
end
|
58
|
-
|
59
|
-
def all_combinations(length_range=1..self.length)
|
60
|
-
permutations = []
|
61
|
-
length_range.max.downto(length_range.min) do |length|
|
62
|
-
self.permutation(length).each do |perm|
|
63
|
-
permutations << perm.sort if !permutations.include?(perm.sort)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
return permutations
|
67
|
-
end
|
68
|
-
|
69
|
-
def structs_to_hashes
|
70
|
-
keys = (self.first.methods-Class.methods).collect{|x| x.to_s.gsub("=", "") if x.to_s.include?("=") && x.to_s!= "[]="}.compact
|
71
|
-
hashed_set = []
|
72
|
-
self.each do |struct|
|
73
|
-
object = {}
|
74
|
-
keys.collect{|k| object[k] = k.class == DateTime ? struct.send(k).to_time : struct.send(k)}
|
75
|
-
hashed_set << object
|
76
|
-
end
|
77
|
-
return hashed_set
|
78
|
-
end
|
79
|
-
|
80
|
-
def sth
|
81
|
-
structs_to_hashes
|
82
|
-
end
|
83
|
-
end
|
data/fixnum.rb
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
class Fixnum
|
2
|
-
|
3
|
-
def days
|
4
|
-
return self*60*60*24
|
5
|
-
end
|
6
|
-
|
7
|
-
def day
|
8
|
-
return days
|
9
|
-
end
|
10
|
-
|
11
|
-
def weeks
|
12
|
-
return self*60*60*24*7
|
13
|
-
end
|
14
|
-
|
15
|
-
def week
|
16
|
-
return weeks
|
17
|
-
end
|
18
|
-
|
19
|
-
def generalized_time_factor
|
20
|
-
if self < 60
|
21
|
-
#one second
|
22
|
-
return 1
|
23
|
-
elsif self < 3600
|
24
|
-
#one minute
|
25
|
-
return 60
|
26
|
-
elsif self < 86400
|
27
|
-
#one hour
|
28
|
-
return 3600
|
29
|
-
elsif self < 604800
|
30
|
-
#one day
|
31
|
-
return 86400
|
32
|
-
elsif self < 11536000
|
33
|
-
#one week
|
34
|
-
return 604800
|
35
|
-
else
|
36
|
-
#four weeks
|
37
|
-
return 2419200
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
data/hash.rb
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
class Hash
|
2
|
-
def flat_each(prefix=[], &blk)
|
3
|
-
each do |k,v|
|
4
|
-
if v.is_a?(Hash)
|
5
|
-
v.flat_each(prefix+[k], &blk)
|
6
|
-
else
|
7
|
-
yield prefix+[k], v
|
8
|
-
end
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
def flatify
|
13
|
-
hh = {}
|
14
|
-
self.to_enum(:flat_each).collect { |k,v| [k.join("-"),v] }.collect {|attrib| hh[attrib[0]] = attrib[1]}
|
15
|
-
return hh
|
16
|
-
end
|
17
|
-
|
18
|
-
def highest
|
19
|
-
high_pair = self.max {|a,b| a[1] <=> b[1]}
|
20
|
-
return {high_pair[0] => high_pair[1]}
|
21
|
-
end
|
22
|
-
|
23
|
-
def lowest
|
24
|
-
low_pair = self.min {|a,b| a[1] <=> b[1]}
|
25
|
-
return {low_pair[0] => low_pair[1]}
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.zip(keys, values, default=nil, &block)
|
29
|
-
hsh = block_given? ? Hash.new(&block) : Hash.new(default)
|
30
|
-
keys.zip(values) { |k,v| hsh[k]=v }
|
31
|
-
hsh
|
32
|
-
end
|
33
|
-
end
|
data/nil_class.rb
DELETED
data/string.rb
DELETED
@@ -1,68 +0,0 @@
|
|
1
|
-
class String
|
2
|
-
|
3
|
-
require 'rubygems'
|
4
|
-
require 'htmlentities'
|
5
|
-
|
6
|
-
def write(str)
|
7
|
-
self << str
|
8
|
-
end
|
9
|
-
|
10
|
-
def underscore
|
11
|
-
self.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').tr("-", "_").downcase
|
12
|
-
end
|
13
|
-
|
14
|
-
def pluralize
|
15
|
-
self.underscore.concat("s")
|
16
|
-
end
|
17
|
-
|
18
|
-
def sanitize_for_streaming
|
19
|
-
# return self.split("").reject {|c| c.match(/[\w\'\-]/).nil?}.to_s
|
20
|
-
return self.gsub(/[\'\"]/, '').gsub("#", "%23").gsub(' ', '%20')
|
21
|
-
end
|
22
|
-
|
23
|
-
def classify
|
24
|
-
if self.split(//).last == "s"
|
25
|
-
if self.split(//)[self.split(//).length-3..self.split(//).length].join == "ies"
|
26
|
-
camelize(self.split(//)[0..self.split(//).length-4].join("")+"y")
|
27
|
-
else
|
28
|
-
camelize(self.sub(/.*\./, '').chop)
|
29
|
-
end
|
30
|
-
else
|
31
|
-
camelize(self.sub(/.*\./, ''))
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
|
36
|
-
if first_letter_in_uppercase
|
37
|
-
lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def constantize
|
42
|
-
return Object.const_defined?(self) ? Object.const_get(self) : Object.const_missing(self)
|
43
|
-
end
|
44
|
-
|
45
|
-
def to_class
|
46
|
-
return self.classify.constantize
|
47
|
-
end
|
48
|
-
|
49
|
-
def super_strip
|
50
|
-
#This regexp is used in place of \W to allow for # and @ signs.
|
51
|
-
if self.include?("#") || self.include?("@")
|
52
|
-
return self
|
53
|
-
elsif self.include?("http")
|
54
|
-
return self
|
55
|
-
else
|
56
|
-
return self.strip.downcase.gsub(/[!$%\*&:.\;{}\[\]\(\)\-\_+=\'\"\|<>,\/?~`]/, "")
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def super_split(split_char)
|
61
|
-
#This regexp is used in place of \W to allow for # and @ signs.
|
62
|
-
return self.gsub(/[!$%\*\;{}\[\]\(\)\+=\'\"\|<>,~`]/, " ").split(split_char)
|
63
|
-
end
|
64
|
-
|
65
|
-
def blank?
|
66
|
-
return self.empty? || self.nil?
|
67
|
-
end
|
68
|
-
end
|