Class: Array2D

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

Overview

/****************************************************************************/ /* clase Array2D */ /* */ /* Clase para Manejar Arreglos de 2 Dimensiones */ /****************************************************************************/ /*

Version: 1.0 en ruby 1.8.x
Fecha  : 28 de Marzo 2008
Autor  : by Greg Brown
http   : http://rubyquiz.com/quiz33.html

*/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(w, h, defel = nil) ⇒ Array2D

Returns a new instance of Array2D.



15
16
17
18
# File 'lib/Array2D.rb', line 15

def initialize(w, h, defel=nil)
 @w, @h=w, h
 @array=Array.new(w*h, defel)
end

Instance Attribute Details

#hObject (readonly)

Returns the value of attribute h.



14
15
16
# File 'lib/Array2D.rb', line 14

def h
  @h
end

#wObject (readonly)

Returns the value of attribute w.



14
15
16
# File 'lib/Array2D.rb', line 14

def w
  @w
end

Instance Method Details

#[](x, y) ⇒ Object



19
20
21
# File 'lib/Array2D.rb', line 19

def [](x, y)
 @array[(y % h) * w + (x % w)]
end

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



22
23
24
# File 'lib/Array2D.rb', line 22

def []=(x, y, v)
 @array[(y % h) * w + (x % w)]=v
end

#to_sObject



25
26
27
28
29
30
31
32
# File 'lib/Array2D.rb', line 25

def to_s
 (0...h).collect { |y|
  (0...w).collect { |x|
   v=self[x, y]
   block_given? ? yield(v) : v
  }.join " "
 }.join "\n"
end