Class: Array2dSimple::Array2d

Inherits:
Object
  • Object
show all
Defined in:
lib/array_2d_simple/array_2d.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Array2d

Returns a new instance of Array2d.



7
8
9
10
11
12
13
14
15
# File 'lib/array_2d_simple/array_2d.rb', line 7

def initialize params
  raise 'width must be set' unless params[:width]
  raise 'height must be set' unless params[:height]

  params.each do |key,value|
    instance_variable_set("@#{key}",value)
  end
  @elements = Array.new(@height){ Array.new(@width) }
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



5
6
7
# File 'lib/array_2d_simple/array_2d.rb', line 5

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



5
6
7
# File 'lib/array_2d_simple/array_2d.rb', line 5

def width
  @width
end

Instance Method Details

#[](x, y) ⇒ Object



37
38
39
# File 'lib/array_2d_simple/array_2d.rb', line 37

def [] x,y
  get_by_rc y,x
end

#[]=(x, y, value) ⇒ Object



41
42
43
# File 'lib/array_2d_simple/array_2d.rb', line 41

def []= x,y, value
  @elements[y][x] = value
end

#each(&proc) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/array_2d_simple/array_2d.rb', line 97

def each &proc
  (0..@height - 1).each do |r|
    (0..@width - 1).each do |c|
      @elements[r][c] = proc.call(@elements[r][c]) if @elements[r][c].class == Float
    end
  end
end

#format2(value) ⇒ Object

Helper methods



106
107
108
# File 'lib/array_2d_simple/array_2d.rb', line 106

def format2 value
  "%0.2f" % value.round(2).to_s if value.finite?
end

#get_by_rc(r, c) ⇒ Object



21
22
23
24
25
# File 'lib/array_2d_simple/array_2d.rb', line 21

def get_by_rc r,c
  raise 'x out of range' if c >= @width
  raise 'y out of range' if r >= @height
  @elements[r][c]
end

#get_by_xy(x, y) ⇒ Object



33
34
35
# File 'lib/array_2d_simple/array_2d.rb', line 33

def get_by_xy x,y
  get_by_rc y,x
end

#maxObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/array_2d_simple/array_2d.rb', line 57

def max
  max = -999999
  (0..@height - 1).each do |r|
    (0..@width - 1).each do |c|
      value = get_by_rc(r,c).to_f if get_by_rc(r,c).respond_to?(:to_f)
      max = [max, value].max if value and ! value.to_f.nan?
    end
  end
  max
end

#minObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/array_2d_simple/array_2d.rb', line 68

def min
  min = 999999
  (0..@height - 1).each do |r|
    (0..@width - 1).each do |c|
      value = get_by_rc(r,c).to_f if value.respond_to?(:to_f)
      min = [min, value].min if value and ! value.to_f.nan?
    end
  end
  min
end

#outputObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/array_2d_simple/array_2d.rb', line 45

def output
  size = format2(max.to_f).size
  output = ''
  (0..@height - 1).each do |r|
    (0..@width - 1).each do |c|
      output += "%#{ size + 4 }s" % format2(get_by_rc(r,c).to_f) if get_by_rc(r,c).respond_to? :to_f
    end
    output += "\n"
  end
  output
end

#project(&proc) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/array_2d_simple/array_2d.rb', line 79

def project &proc
  new_array = self.class.new( width: @width, height: @height )
  (0..@height - 1).each do |r|
    (0..@width - 1).each do |c|
      new_array.set_by_rc(r,c, proc.call(get_by_rc(r,c)))
    end
  end
  new_array
end

#set_by_rc(r, c, value) ⇒ Object



27
28
29
30
31
# File 'lib/array_2d_simple/array_2d.rb', line 27

def set_by_rc r,c, value
  raise 'x out of range' if c >= @width
  raise 'y out of range' if r >= @height
  @elements[r][c] = value
end

#set_each(&proc) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/array_2d_simple/array_2d.rb', line 89

def set_each &proc
  (0..@height - 1).each do |r|
    (0..@width - 1).each do |c|
      @elements[r][c] = proc.call(r,c)
    end
  end
end

#to_sObject



17
18
19
# File 'lib/array_2d_simple/array_2d.rb', line 17

def to_s
  "#{ self.class }: #{ @width }x#{ @height }"
end