array_2d_simple 0.2.2 → 0.3.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/Guardfile CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  guard 'rspec' do
5
5
  watch(%r{^spec/.+_spec\.rb$})
6
- watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
6
+ watch(%r{^lib/array_2d_simple/(.+)\.rb$}) { 'spec' }
7
7
  watch('spec/spec_helper.rb') { "spec" }
8
8
 
9
9
  # Rails example
@@ -2,6 +2,7 @@ require "array_2d_simple/version"
2
2
 
3
3
  module Array2dSimple
4
4
  class Array2d
5
+ include Enumerable
5
6
  attr_reader :width, :height
6
7
 
7
8
  def initialize params
@@ -19,93 +20,95 @@ module Array2dSimple
19
20
  end
20
21
 
21
22
  def get_by_rc r,c
22
- raise 'x out of range' if c >= @width
23
- raise 'y out of range' if r >= @height
24
- @elements[r][c]
23
+ Printer.log 'Warning: #get_by_rc is depricated. Use get_by_xy. Note: arguments are reversed with get_by_xy'
24
+ get_by_xy(c,r)
25
25
  end
26
26
 
27
27
  def set_by_rc r,c, value
28
- raise 'x out of range' if c >= @width
29
- raise 'y out of range' if r >= @height
30
- @elements[r][c] = value
28
+ Printer.log 'Warning: #set_by_rc is depricated. Use set_by_xy. Note: arguments are reversed with set_by_xy'
29
+ set_by_xy(c,r,value)
31
30
  end
32
31
 
33
32
  def get_by_xy x,y
34
- get_by_rc y,x
33
+ raise 'x out of range' if x >= @width
34
+ raise 'y out of range' if y >= @height
35
+ @elements[y][x]
36
+ end
37
+
38
+ def set_by_xy(x,y,value)
39
+ raise 'x out of range' if x >= @width
40
+ raise 'y out of range' if y >= @height
41
+ @elements[y][x] = value
35
42
  end
36
43
 
37
44
  def [] x,y
38
- get_by_rc y,x
45
+ get_by_xy(x,y)
39
46
  end
40
47
 
41
48
  def []= x,y, value
42
- @elements[y][x] = value
49
+ set_by_xy(x,y,value)
43
50
  end
44
51
 
45
52
  def output
46
53
  size = format2(max.to_f).size
47
54
  output = ''
55
+
48
56
  (0..@height - 1).each do |r|
49
57
  (0..@width - 1).each do |c|
50
58
  output += "%#{ size + 4 }s" % format2(get_by_rc(r,c).to_f) if get_by_rc(r,c).respond_to? :to_f
51
59
  end
52
60
  output += "\n"
53
61
  end
62
+
54
63
  output
55
64
  end
56
65
 
57
66
  def max
58
- max = -999999
59
- (0..@height - 1).each do |r|
60
- (0..@width - 1).each do |c|
61
- value = get_by_rc(r,c).to_f if get_by_rc(r,c).respond_to?(:to_f)
62
- max = [max, value].max if value and ! value.to_f.nan?
63
- end
64
- end
65
- max
67
+ @elements.dup.flatten.max
66
68
  end
67
69
 
68
70
  def min
69
- min = 999999
70
- (0..@height - 1).each do |r|
71
- (0..@width - 1).each do |c|
72
- value = get_by_rc(r,c).to_f if value.respond_to?(:to_f)
73
- min = [min, value].min if value and ! value.to_f.nan?
74
- end
75
- end
76
- min
71
+ @elements.dup.flatten.min
77
72
  end
78
73
 
79
74
  def project &proc
80
75
  new_array = self.class.new( width: @width, height: @height )
81
- (0..@height - 1).each do |r|
82
- (0..@width - 1).each do |c|
83
- new_array.set_by_rc(r,c, proc.call(get_by_rc(r,c)))
84
- end
85
- end
76
+
77
+ new_array.set_each{ |x, y| proc.call( get_by_xy( y,x ))}
78
+
86
79
  new_array
87
80
  end
88
81
 
89
82
  def set_each &proc
90
- (0..@height - 1).each do |r|
91
- (0..@width - 1).each do |c|
92
- @elements[r][c] = proc.call(r,c)
93
- end
83
+ each_with_index do |v,i|
84
+ x = i[0]
85
+ y = i[1]
86
+ @elements[x][y] = proc.call(x,y)
94
87
  end
95
88
  end
96
89
 
97
- def each &proc
98
- (0..@height - 1).each do |r|
99
- (0..@width - 1).each do |c|
100
- @elements[r][c] = proc.call(@elements[r][c]) if @elements[r][c].class == Float
90
+ def each_with_index
91
+ (0..@width - 1).each do |y|
92
+ (0..@height - 1).each do |x|
93
+ yield(@elements[x][y],[x,y])
101
94
  end
102
95
  end
103
96
  end
104
97
 
98
+ def each &block
99
+ each_with_index &block
100
+ end
101
+
105
102
  # Helper methods
106
103
  def format2 value
107
104
  "%0.2f" % value.round(2).to_s if value.finite?
108
105
  end
106
+ end
107
+ end
108
+
109
109
 
110
+ class Printer
111
+ def self.log(message)
112
+ puts message
110
113
  end
111
114
  end
@@ -1,3 +1,3 @@
1
1
  module Array2dSimple
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -4,6 +4,10 @@ module Array2dSimple
4
4
  describe Array2d do
5
5
  let(:array_2d) { Array2d.new(width:8, height: 2) }
6
6
 
7
+ before do
8
+ Printer.stub( :log )
9
+ end
10
+
7
11
  it 'creates' do
8
12
  array_2d.class.should eq Array2d
9
13
  end
@@ -13,26 +17,45 @@ module Array2dSimple
13
17
  big_array.to_s.should include '30x100'
14
18
  end
15
19
 
20
+ describe 'enumerable' do
21
+ it 'each calls the block with the current value' do
22
+ a2x2 = Array2d.new(width: 2, height: 2)
23
+ a2x2.set_by_xy(0,0,1)
24
+ a2x2.set_by_xy(0,1,2)
25
+ a2x2.set_by_xy(1,0,3)
26
+ a2x2.set_by_xy(1,1,4)
27
+
28
+ foo = []
29
+ a2x2.each { |element| foo << element }
30
+ foo.should == [1,2,3,4]
31
+ end
32
+ end
33
+
16
34
  context 'shared' do
35
+ let( :getter_warning ){ 'Warning: #get_by_rc is depricated. Use get_by_xy. Note: arguments are reversed with get_by_xy'}
36
+ let( :setter_warning ){ 'Warning: #set_by_rc is depricated. Use set_by_xy. Note: arguments are reversed with set_by_xy'}
37
+
17
38
  before do
18
- proc = Proc.new { |r, c| r * 8 + c + 1}
39
+ proc = Proc.new { |x, y| x * 8 + y + 1}
19
40
  array_2d.set_each &proc
20
41
  end
21
42
 
22
43
  it 'fills Array2d' do
23
- array_2d.get_by_rc(0,0).should eq 1
24
- array_2d.get_by_rc(0,7).should eq 8
25
- array_2d.get_by_rc(1,1).should eq 10
26
- array_2d.get_by_rc(1,3).should eq 12
27
- array_2d.get_by_rc(1,7).should eq 16
44
+ array_2d.get_by_xy(0,0).should eq 1
45
+ array_2d.get_by_xy(7,0).should eq 8
46
+ array_2d.get_by_xy(1,1).should eq 10
47
+ array_2d.get_by_xy(3,1).should eq 12
48
+ array_2d.get_by_xy(7,1).should eq 16
28
49
  end
29
50
 
30
- it 'gets by rc' do
51
+ it 'gets by rc is deprecated' do
52
+ Printer.should_receive( :log ).with getter_warning
31
53
  array_2d.get_by_rc(1,4).should eq 13
32
54
  end
33
55
 
34
- it 'gets_by_r_c' do
35
- array_2d.get_by_rc(1,5).should eq 14
56
+ it 'sets by rc is deprecated' do
57
+ Printer.should_receive( :log ).with setter_warning
58
+ array_2d.set_by_rc(1,4, 13).should eq 13
36
59
  end
37
60
 
38
61
  it 'gets_by_x_y' do
@@ -4,8 +4,12 @@ module Array2dSimple
4
4
  describe NormalizedArray2d do
5
5
  let(:array_2d) { Array2d.new(width:8, height:2) }
6
6
 
7
+ before do
8
+ Printer.stub( :log )
9
+ end
10
+
7
11
  it 'gets normalized value' do
8
- array_2d.set_each { |r, c| r * 8 + c + 1}
12
+ array_2d.set_each { |x, y| x * 8 + y + 1}
9
13
  normalized_array = NormalizedArray2d.new(array_2d)
10
14
  normalized_array[0,0].should eq 0.0
11
15
  normalized_array[3,0].should eq 0.2
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: array_2d_simple
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-13 00:00:00.000000000 Z
12
+ date: 2013-09-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec