GFunk911-mharris_ext 1.1.0

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/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 1
3
+ :patch: 0
4
+ :major: 1
@@ -0,0 +1,6 @@
1
+ def tm(msg="Thing")
2
+ t = Time.now
3
+ yield
4
+ seconds = Time.now - t
5
+ puts "#{msg} took #{seconds} seconds"
6
+ end
@@ -0,0 +1,6 @@
1
+ def ec(cmd)
2
+ puts cmd
3
+ res = `#{cmd}`
4
+ puts res
5
+ res
6
+ end
@@ -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
@@ -0,0 +1,10 @@
1
+ module FromHash
2
+ def from_hash(ops)
3
+ ops.each do |k,v|
4
+ send("#{k}=",v)
5
+ end
6
+ end
7
+ def initialize(ops={})
8
+ from_hash(ops)
9
+ end
10
+ end
@@ -0,0 +1,2 @@
1
+ require 'facets/file/write'
2
+ require 'fattr'
@@ -0,0 +1,6 @@
1
+ class Object
2
+ def local_methods
3
+ res = methods - 7.methods - "".methods
4
+ res.sort_by { |x| x.to_s }
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ class Regexp
2
+ def self.escaped(str)
3
+ new(escape(str))
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ glob = File.dirname(__FILE__) + "/mharris_ext/*.rb"
2
+ Dir[glob].each { |x| require x }
@@ -0,0 +1,7 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class MharrisExtTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'mharris_ext'
8
+
9
+ class Test::Unit::TestCase
10
+ end
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
+