GFunk911-mharris_ext 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +4 -0
- data/lib/mharris_ext/benchmark.rb +6 -0
- data/lib/mharris_ext/cmd.rb +6 -0
- data/lib/mharris_ext/enumerable.rb +111 -0
- data/lib/mharris_ext/fileutils.rb +52 -0
- data/lib/mharris_ext/from_hash.rb +10 -0
- data/lib/mharris_ext/gems.rb +2 -0
- data/lib/mharris_ext/methods.rb +6 -0
- data/lib/mharris_ext/regexp.rb +5 -0
- data/lib/mharris_ext.rb +2 -0
- data/test/mharris_ext_test.rb +7 -0
- data/test/test_helper.rb +10 -0
- metadata +66 -0
data/VERSION.yml
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
class Fixnum
|
2
|
+
def of
|
3
|
+
res = []
|
4
|
+
self.times { res << yield }
|
5
|
+
res
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
module Enumerable
|
10
|
+
def sum
|
11
|
+
res = 0
|
12
|
+
each { |x| res += x }
|
13
|
+
res
|
14
|
+
end
|
15
|
+
def sum_b
|
16
|
+
map { |x| yield(x) }.sum
|
17
|
+
end
|
18
|
+
def avg_b(&b)
|
19
|
+
sum_b(&b).to_f / size.to_f
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
module Enumerable
|
24
|
+
def nths_hashx(num)
|
25
|
+
res = {}
|
26
|
+
s = e_f = e_i = 0
|
27
|
+
n_size = size.to_f / num.to_f
|
28
|
+
total_segment_size = 0
|
29
|
+
(0...num).each do |n|
|
30
|
+
s = e_i
|
31
|
+
e_f = e_f + n_size
|
32
|
+
e_i = (e_f+0.5).to_i
|
33
|
+
seg = self[s...e_i]
|
34
|
+
# puts %w(s e_f e_i seg).map { |x| "#{x}: #{send(x)}" }.join("\n")
|
35
|
+
#puts "s: #{s}, e_f: #{e_f}, e_i: #{e_i}, seg: #{seg.inspect}"
|
36
|
+
res[n] = seg
|
37
|
+
total_segment_size += res[n].size
|
38
|
+
end
|
39
|
+
unless total_segment_size == size
|
40
|
+
puts inspect
|
41
|
+
puts res.inspect
|
42
|
+
raise "nths_hash is bad"
|
43
|
+
end
|
44
|
+
res
|
45
|
+
end
|
46
|
+
def nths(num)
|
47
|
+
res = []
|
48
|
+
s = e_f = e_i = 0
|
49
|
+
n_size = size.to_f / num.to_f
|
50
|
+
total_segment_size = 0
|
51
|
+
(0...num).each do |n|
|
52
|
+
s = e_i
|
53
|
+
e_f = e_f + n_size
|
54
|
+
e_i = (e_f+0.5).to_i
|
55
|
+
seg = self[s...e_i]
|
56
|
+
# puts %w(s e_f e_i seg).map { |x| "#{x}: #{send(x)}" }.join("\n")
|
57
|
+
#puts "s: #{s}, e_f: #{e_f}, e_i: #{e_i}, seg: #{seg.inspect}"
|
58
|
+
res << seg
|
59
|
+
total_segment_size += res[n].size
|
60
|
+
end
|
61
|
+
unless total_segment_size == size
|
62
|
+
puts inspect
|
63
|
+
puts res.inspect
|
64
|
+
raise "nths_hash is bad"
|
65
|
+
end
|
66
|
+
res
|
67
|
+
end
|
68
|
+
def nths_hash(num)
|
69
|
+
res = {}
|
70
|
+
nths(num).each_with_index { |x,i| res[i] = x }
|
71
|
+
res
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
res = [1,2,3,4,5,6].nths_hash(3)
|
76
|
+
exp = {0 => [1,2], 1 => [3,4], 2 => [5,6]}
|
77
|
+
unless res == exp
|
78
|
+
puts res.inspect
|
79
|
+
puts exp.inspect
|
80
|
+
raise "nths_hash doesn't work"
|
81
|
+
end
|
82
|
+
|
83
|
+
res = [1,2,3,4,5,6,7].nths_hash(3)
|
84
|
+
exp = {0 => [1,2], 1 => [3,4,5], 2 => [6,7]}
|
85
|
+
unless res == exp
|
86
|
+
puts res.inspect
|
87
|
+
puts exp.inspect
|
88
|
+
raise "nths_hash doesn't work"
|
89
|
+
end
|
90
|
+
|
91
|
+
class Hash
|
92
|
+
def map_value
|
93
|
+
res = {}
|
94
|
+
each { |k,v| res[k] = yield(v) }
|
95
|
+
res
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
class Numeric
|
100
|
+
def commify
|
101
|
+
to_s.commify
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
class String
|
106
|
+
def commify
|
107
|
+
return self if length <= 3
|
108
|
+
self[0...-3].commify + "," + self[-3..-1]
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
@@ -0,0 +1,52 @@
|
|
1
|
+
def all_dirs_recursive(dir)
|
2
|
+
raise "null dir" unless dir
|
3
|
+
init_slash = (dir[0..0] == '/')
|
4
|
+
#raise "can't handle initial slash" if dir[0..0] == '/'
|
5
|
+
#puts "all_dirs_recursive #{dir}"
|
6
|
+
dir.split("/")[0..-1].inject([]) do |paths,dir|
|
7
|
+
last_path = (paths.empty? ? "" : "#{paths[-1]}/")
|
8
|
+
paths + ["#{last_path}#{dir}"]
|
9
|
+
end.select { |x| x != '' }
|
10
|
+
end
|
11
|
+
|
12
|
+
def mkdir_if(dir)
|
13
|
+
if FileTest.exists?(dir)
|
14
|
+
#puts "not making #{dir}"
|
15
|
+
else
|
16
|
+
#puts "making #{dir}"
|
17
|
+
FileUtils.mkdir(dir)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def mkdir_recursive(ops)
|
22
|
+
dir = ops.is_a?(Hash) ? ops[:dir] : ops
|
23
|
+
#puts "mkdir_recursive #{dir}"
|
24
|
+
all_dirs_recursive(dir).each do |dir|
|
25
|
+
mkdir_if(dir)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def mv_making_dir(f,new_dir)
|
30
|
+
#puts "mv_making_dir #{f} #{new_dir}"
|
31
|
+
mkdir_recursive(new_dir)
|
32
|
+
FileUtils.mv(f,new_dir)
|
33
|
+
end
|
34
|
+
|
35
|
+
unless all_dirs_recursive("a/b/c") == ["a","a/b","a/b/c"]
|
36
|
+
res = all_dirs_recursive("a/b/c")
|
37
|
+
raise "all_dirs_recursive doesn't work #{res.inspect}"
|
38
|
+
end
|
39
|
+
|
40
|
+
unless all_dirs_recursive("/a/b/c") == ["/a","/a/b","/a/b/c"]
|
41
|
+
res = all_dirs_recursive("/a/b/c")
|
42
|
+
raise "all_dirs_recursive doesn't work #{res.inspect}"
|
43
|
+
end
|
44
|
+
|
45
|
+
def rm_r_if(dir)
|
46
|
+
FileUtils.rm_r(dir) if FileTest.exists?(dir)
|
47
|
+
end
|
48
|
+
|
49
|
+
def eat_exceptions
|
50
|
+
yield
|
51
|
+
rescue
|
52
|
+
end
|
data/lib/mharris_ext.rb
ADDED
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: GFunk911-mharris_ext
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Harris
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-03-06 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: TODO
|
17
|
+
email: GFunk913@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- VERSION.yml
|
26
|
+
- lib/mharris_ext
|
27
|
+
- lib/mharris_ext/benchmark.rb
|
28
|
+
- lib/mharris_ext/cmd.rb
|
29
|
+
- lib/mharris_ext/enumerable.rb
|
30
|
+
- lib/mharris_ext/fileutils.rb
|
31
|
+
- lib/mharris_ext/from_hash.rb
|
32
|
+
- lib/mharris_ext/gems.rb
|
33
|
+
- lib/mharris_ext/methods.rb
|
34
|
+
- lib/mharris_ext/regexp.rb
|
35
|
+
- lib/mharris_ext.rb
|
36
|
+
- test/mharris_ext_test.rb
|
37
|
+
- test/test_helper.rb
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: http://github.com/GFunk911/mharris_ext
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options:
|
42
|
+
- --inline-source
|
43
|
+
- --charset=UTF-8
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.2.0
|
62
|
+
signing_key:
|
63
|
+
specification_version: 2
|
64
|
+
summary: TODO
|
65
|
+
test_files: []
|
66
|
+
|