useful_class_extensions 0.0.1
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/array.rb +83 -0
- data/fixnum.rb +41 -0
- data/hash.rb +28 -0
- data/nil_class.rb +9 -0
- data/string.rb +68 -0
- data/time.rb +9 -0
- metadata +52 -0
data/array.rb
ADDED
@@ -0,0 +1,83 @@
|
|
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
ADDED
@@ -0,0 +1,41 @@
|
|
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
ADDED
@@ -0,0 +1,28 @@
|
|
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
|
+
end
|
data/nil_class.rb
ADDED
data/string.rb
ADDED
@@ -0,0 +1,68 @@
|
|
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
|
data/time.rb
ADDED
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: useful_class_extensions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Devin Gaffney
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-14 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: A set of useful small class extensions to do basic things that aren't
|
15
|
+
for some reason already part of ruby or rails.
|
16
|
+
email: itsme@devingaffney.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- array.rb
|
22
|
+
- fixnum.rb
|
23
|
+
- hash.rb
|
24
|
+
- nil_class.rb
|
25
|
+
- string.rb
|
26
|
+
- time.rb
|
27
|
+
homepage: http://devingaffney.com
|
28
|
+
licenses: []
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
requirements: []
|
46
|
+
rubyforge_project:
|
47
|
+
rubygems_version: 1.8.10
|
48
|
+
signing_key:
|
49
|
+
specification_version: 3
|
50
|
+
summary: I keep re-using these little additional primitive extensions all the time.
|
51
|
+
So I made a gem.
|
52
|
+
test_files: []
|