epitools 0.1.2 → 0.1.3

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 CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{epitools}
8
- s.version = "0.1.2"
8
+ s.version = "0.1.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["epitron"]
@@ -35,12 +35,14 @@ Gem::Specification.new do |s|
35
35
  "lib/epitools/pretty_backtrace.rb",
36
36
  "lib/epitools/rails.rb",
37
37
  "lib/epitools/rash.rb",
38
+ "lib/epitools/ratio.rb",
38
39
  "lib/epitools/string_to_proc.rb",
39
40
  "spec/basetypes_spec.rb",
40
41
  "spec/lcs_spec.rb",
41
42
  "spec/metaclass_spec.rb",
42
43
  "spec/permutations_spec.rb",
43
44
  "spec/rash_spec.rb",
45
+ "spec/ratio_spec.rb",
44
46
  "spec/spec.opts",
45
47
  "spec/spec_helper.rb"
46
48
  ]
@@ -50,12 +52,13 @@ Gem::Specification.new do |s|
50
52
  s.rubygems_version = %q{1.3.6}
51
53
  s.summary = %q{NOT UTILS... METILS!}
52
54
  s.test_files = [
53
- "spec/lcs_spec.rb",
55
+ "spec/permutations_spec.rb",
54
56
  "spec/rash_spec.rb",
55
- "spec/metaclass_spec.rb",
56
- "spec/permutations_spec.rb",
57
57
  "spec/spec_helper.rb",
58
- "spec/basetypes_spec.rb"
58
+ "spec/lcs_spec.rb",
59
+ "spec/basetypes_spec.rb",
60
+ "spec/ratio_spec.rb",
61
+ "spec/metaclass_spec.rb"
59
62
  ]
60
63
 
61
64
  if s.respond_to? :specification_version then
@@ -6,6 +6,7 @@ __DIR__ = File.dirname(__FILE__)
6
6
  niceprint
7
7
  string_to_proc
8
8
  permutations
9
+ ratio
9
10
  ].each do |mod|
10
11
  require File.join(__DIR__, "epitools", mod)
11
12
  end
@@ -5,12 +5,10 @@ class Object
5
5
  def integer?; false; end
6
6
  end
7
7
 
8
-
9
8
  class Float
10
9
  def integer?; true; end
11
10
  end
12
11
 
13
-
14
12
  class String
15
13
 
16
14
  #
@@ -20,6 +18,27 @@ class String
20
18
  self.strip.match(/^\d+$/) ? true : false
21
19
  end
22
20
 
21
+ #
22
+ # Convert \r\n to \n
23
+ #
24
+ def to_unix
25
+ gsub("\r\n", "\n")
26
+ end
27
+
28
+ #
29
+ # Remove redundant whitespaces (not including newlines).
30
+ #
31
+ def tighten
32
+ gsub(/[\t ]+/,' ').strip
33
+ end
34
+
35
+ #
36
+ # Remove redundant whitespace AND newlines.
37
+ #
38
+ def dewhitespace
39
+ gsub(/\s+/,' ').strip
40
+ end
41
+
23
42
  #
24
43
  # Like #lines, but skips empty lines and removes \n's.
25
44
  #
@@ -31,7 +50,6 @@ class String
31
50
 
32
51
  end
33
52
 
34
-
35
53
  class Integer
36
54
 
37
55
  def integer?
@@ -97,12 +115,13 @@ module Enumerable
97
115
  include_boundary = options[:include_boundary] || false
98
116
 
99
117
  if matcher.nil?
100
- boundary_test = block
118
+ boundary_test_proc = block
101
119
  else
102
120
  if matcher.is_a? String or matcher.is_a? Regexp
103
- boundary_test = proc { |e| e[matcher] }
121
+ boundary_test_proc = proc { |element| element[matcher] rescue nil }
104
122
  else
105
- raise "I don't know how to split with #{matcher}"
123
+ boundary_test_proc = proc { |element| element == matcher }
124
+ #raise "I don't know how to split with #{matcher}"
106
125
  end
107
126
  end
108
127
 
@@ -111,7 +130,7 @@ module Enumerable
111
130
 
112
131
  each do |e|
113
132
 
114
- if boundary_test.call(e)
133
+ if boundary_test_proc.call(e)
115
134
 
116
135
  if current_chunk.empty? and not include_boundary
117
136
  next # hit 2 boundaries in a row... just keep moving, people!
@@ -140,8 +159,6 @@ module Enumerable
140
159
  chunks # resultset
141
160
  end
142
161
 
143
- alias_method :split, :split_at
144
-
145
162
  #
146
163
  # Split the array into chunks, with the boundaries being after the element to split on.
147
164
  #
@@ -150,7 +167,7 @@ module Enumerable
150
167
  def split_after(matcher=nil, options={}, &block)
151
168
  options[:after] ||= true
152
169
  options[:include_boundary] ||= true
153
- split(matcher, options, &block)
170
+ split_at(matcher, options, &block)
154
171
  end
155
172
 
156
173
  #
@@ -160,7 +177,7 @@ module Enumerable
160
177
  #
161
178
  def split_before(matcher=nil, options={}, &block)
162
179
  options[:include_boundary] ||= true
163
- split(matcher, options, &block)
180
+ split_at(matcher, options, &block)
164
181
  end
165
182
 
166
183
  #
@@ -298,15 +315,26 @@ end
298
315
 
299
316
 
300
317
  class Hash
318
+
319
+ #
320
+ # Runs remove_blank_lines on self.
321
+ #
301
322
  def remove_blank_values!
302
323
  delete_if{|k,v| v.blank?}
303
324
  self
304
325
  end
305
326
 
327
+ #
328
+ # Returns a new Hash where all elements whose values are "blank?" (eg: "", [], nil)
329
+ # have been eliminated.
330
+ #
306
331
  def remove_blank_values
307
332
  dup.remove_blank_values!
308
333
  end
309
334
 
335
+ #
336
+ # Runs map_values on self.
337
+ #
310
338
  def map_values!(&block)
311
339
  keys.each do |key|
312
340
  value = self[key]
@@ -315,10 +343,16 @@ class Hash
315
343
  self
316
344
  end
317
345
 
346
+ #
347
+ # Returns a Hash whsoe values have been transformed by the block.
348
+ #
318
349
  def map_values(&block)
319
350
  dup.map_values!(&block)
320
351
  end
321
-
352
+
353
+ #
354
+ # Runs map_keys on self.
355
+ #
322
356
  def map_keys!(&block)
323
357
  keys.each do |key|
324
358
  value = delete(key)
@@ -327,13 +361,34 @@ class Hash
327
361
  self
328
362
  end
329
363
 
364
+ #
365
+ # Returns a new Hash whose keys have been transformed by the block.
366
+ #
330
367
  def map_keys(&block)
331
368
  dup.map_keys!(&block)
332
369
  end
333
370
 
334
- # TODO: Where did slice come from?
335
- #alias_method :filter, :slice
336
- #alias_method :filter!, :slice!
371
+ #
372
+ # Creates an new Hash whose missing items default to [].
373
+ # Good for collecting things!
374
+ #
375
+ # eg:
376
+ # Hash.of_arrays[:yays] << "YAY!"
377
+ #
378
+ def self.of_arrays
379
+ new {|h,k| h[k] = [] }
380
+ end
381
+
382
+ #
383
+ # Creates an new Hash whose missing items default to values of 0.
384
+ # Good for counting things!
385
+ #
386
+ # eg:
387
+ # Hash.of_integers[:yays] += 1
388
+ #
389
+ def self.of_integers
390
+ new(0)
391
+ end
337
392
 
338
393
  end
339
394
 
@@ -8,4 +8,3 @@ if defined? Rails
8
8
 
9
9
  end
10
10
 
11
-
@@ -0,0 +1,48 @@
1
+
2
+ class Ratio
3
+
4
+ include Comparable
5
+
6
+ def <=>(other)
7
+ to_f <=> other.to_f
8
+ end
9
+
10
+ attr_accessor :first, :last
11
+
12
+ def self.[](*args)
13
+ new(*args)
14
+ end
15
+
16
+ def initialize(first, last=1)
17
+ @first = first
18
+ @last = last
19
+ end
20
+
21
+ def to_s
22
+ "#{@first}/#{@last}"
23
+ end
24
+ alias_method :ratio, :to_s
25
+
26
+ def to_f
27
+ if @last == 0
28
+ 0.0
29
+ else
30
+ @first.to_f / @last
31
+ end
32
+ end
33
+
34
+ def percent
35
+ "%0.1f%" % (to_f * 100)
36
+ end
37
+ alias_method :to_percent, :percent
38
+
39
+ def inspect
40
+ "#<Ratio: #{to_s}>"
41
+ end
42
+
43
+ def +(other)
44
+ Ratio.new( first+other.first, last+other.last)
45
+ end
46
+
47
+ end
48
+
@@ -104,6 +104,26 @@ describe Enumerable do
104
104
  "a\nb\n---\nc\nd\n".split_at(/---/).map_recursive(&:strip).should == [ %w[a b], %w[c d] ]
105
105
  end
106
106
 
107
+ it "handles nested things" do
108
+ array = [ [],["a"],"a",[1,2,3] ]
109
+
110
+ lambda {
111
+ array.split_at("a")
112
+ }.should_not raise_error
113
+
114
+ array.split_at("a").should == [ array[0..1], array[3..3] ]
115
+ array.split_at([1,2,3]).should == [ array[0..2] ]
116
+ end
117
+
118
+ it "handles arbitrary objects" do
119
+ arbitrary = Struct.new(:a, :b, :c)
120
+
121
+ particular = arbitrary.new(1,2,3)
122
+ array = [ arbitrary.new, arbitrary.new, particular, arbitrary.new]
123
+
124
+ array.split_at(particular).should == [ array[0..1], array[3..3] ]
125
+ end
126
+
107
127
  it "sums" do
108
128
  [1,2,3,4,5].sum.should == 15
109
129
  end
@@ -0,0 +1,24 @@
1
+ require 'epitools/ratio'
2
+
3
+ describe Ratio do
4
+
5
+ before :each do
6
+ @a = Ratio[1,1]
7
+ @b = Ratio[1,2]
8
+ end
9
+
10
+ it "adds" do
11
+ ( @a + @b ).should == Ratio[2,3]
12
+ end
13
+
14
+ it "floats" do
15
+ @a.to_f.should == 1.0
16
+ @b.to_f.should == 0.5
17
+ end
18
+
19
+ it "percents" do
20
+ @a.percent.should == "100.0%"
21
+ @b.percent.should == "50.0%"
22
+ end
23
+
24
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 2
9
- version: 0.1.2
8
+ - 3
9
+ version: 0.1.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - epitron
@@ -59,12 +59,14 @@ files:
59
59
  - lib/epitools/pretty_backtrace.rb
60
60
  - lib/epitools/rails.rb
61
61
  - lib/epitools/rash.rb
62
+ - lib/epitools/ratio.rb
62
63
  - lib/epitools/string_to_proc.rb
63
64
  - spec/basetypes_spec.rb
64
65
  - spec/lcs_spec.rb
65
66
  - spec/metaclass_spec.rb
66
67
  - spec/permutations_spec.rb
67
68
  - spec/rash_spec.rb
69
+ - spec/ratio_spec.rb
68
70
  - spec/spec.opts
69
71
  - spec/spec_helper.rb
70
72
  has_rdoc: true
@@ -98,9 +100,10 @@ signing_key:
98
100
  specification_version: 3
99
101
  summary: NOT UTILS... METILS!
100
102
  test_files:
101
- - spec/lcs_spec.rb
102
- - spec/rash_spec.rb
103
- - spec/metaclass_spec.rb
104
103
  - spec/permutations_spec.rb
104
+ - spec/rash_spec.rb
105
105
  - spec/spec_helper.rb
106
+ - spec/lcs_spec.rb
106
107
  - spec/basetypes_spec.rb
108
+ - spec/ratio_spec.rb
109
+ - spec/metaclass_spec.rb