# Two Dimensional Array

**URL:** <https://www.ruby-forum.com/t/two-dimensional-array/141633>\
**Category:** Ruby\
**Created:** [June 28, 2008, 12:25am UTC](https://www.ruby-forum.com/t/two-dimensional-array/141633 "2008-06-28T00:25:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 28, 2008, 12:25am UTC](https://www.ruby-forum.com/t/two-dimensional-array/141633/1 "2008-06-28T00:25:38Z")

</div>

class Tda

def initialize(width=0, height=0)  
if((width==0) && width\<height)  
raise NotImplementedError,  
“(#{width}) width Tda cannot have a height.”  
else  
@tda = Array.new(width).map!{ Array.new(height) }  
end  
end

def width?  
return @tda.size  
end

def height?  
if(width?\>0)  
return @tda[0].size  
else  
return nil  
end  
end

#–Returns the dimensional size (x x y) as a string  
def size  
w=@tda.size

```
if(width?>0)
  h = @tda[0].size
else
  h = 0
end

return "#{w}x#{h}"

```

end

#–Returns true if both Tda’s have equal widths and heights  
def eql\_size?(tda)  
bool=false

```
lh=size.split('x')
rh=tda.size.split('x')

if(lh[0]==rh[0])
  bool=true
end

if(lh[1]==rh[1])
  bool=true
else
  bool=false
end

return bool

```

end

end

This is what I’ve got so far, but I can’t seem to figure out how to  
write methods for: copying a Tda to another Tda, and indexing the Tda.  
Thanks for the help!

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 29, 2008, 6:50pm UTC](https://www.ruby-forum.com/t/two-dimensional-array/141633/2 "2008-06-29T18:50:02Z")

</div>

Justin To wrote:

> class Tda
> 
> def initialize(width=0, height=0)  
> if((width==0) && width\<height)  
> raise NotImplementedError,  
> “(#{width}) width Tda cannot have a height.”  
> else  
> @tda = Array.new(width).map!{ Array.new(height) }  
> end  
> end
> 
> def width?  
> return @tda.size  
> end
> 
> def height?  
> if(width?\>0)  
> return @tda[0].size  
> else  
> return nil  
> end  
> end
> 
> #–Returns the dimensional size (x x y) as a string  
> def size  
> w=@tda.size
> 
> ```
> if(width?>0)
> h = @tda[0].size
> else
> h = 0
> end
> 
> return "#{w}x#{h}"
> 
> ```
> 
> end
> 
> #–Returns true if both Tda’s have equal widths and heights  
> def eql\_size?(tda)  
> bool=false
> 
> ```
> lh=size.split('x')
> rh=tda.size.split('x')
> 
> if(lh[0]==rh[0])
> bool=true
> end
> 
> if(lh[1]==rh[1])
> bool=true
> else
> bool=false
> end
> 
> return bool
> 
> ```
> 
> end
> 
> end
> 
> This is what I’ve got so far, but I can’t seem to figure out how to  
> write methods for: copying a Tda to another Tda, and indexing the Tda.  
> Thanks for the help!

I haven’t tried this, but this method might help -

def copy\_tda(original\_array)  
new\_tda = []  
original\_array.each\_index do |i|  
new\_a = []  
original\_array[i].each\_index { |j| new\_a \<\<  
original\_array[i][j].clone }  
new\_tda \<\< new\_a  
end  
return new\_tda  
end

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 29, 2008, 10:43pm UTC](https://www.ruby-forum.com/t/two-dimensional-array/141633/3 "2008-06-29T22:43:31Z")

</div>

On Fri, Jun 27, 2008 at 5:23 PM, Justin To [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This is what I’ve got so far, but I can’t seem to figure out how to  
> write methods for: copying a Tda to another Tda, and indexing the Tda.

Marshal.dump and Marshal.load might help you.

Todd

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 30, 2008, 2:12am UTC](https://www.ruby-forum.com/t/two-dimensional-array/141633/4 "2008-06-30T02:12:53Z")

</div>

Hi –

On Sat, 28 Jun 2008, Justin To wrote:

> if(lh[1]==rh[1])  
> bool=true  
> else  
> bool=false  
> end
> 
> return bool  
> end

See Ara Howard’s advice about using narray. Still – I just wanted to  
suggest one or two things to make this method more concise and clear.

One thing you could do is:

def eql\_size?(tda)  
lh=size.split(‘x’)  
rh=tda.size.split(‘x’)

```
 return lh[0] == rh[0] && lh[1] == rh[1]

```

end

In general, it’s good to let the comparison methods, like ==, do their  
thing, which is to produce true and false. There’s usually no need to  
store and test the results separately.

Moreover, in this particular case, I think you could just do:

def eql\_size?(tda)  
size == tda.size  
end

🙂

David

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 30, 2008, 2:33am UTC](https://www.ruby-forum.com/t/two-dimensional-array/141633/5 "2008-06-30T02:33:57Z")

</div>

Thanks for all the help!

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 30, 2008, 1:57am UTC](https://www.ruby-forum.com/t/two-dimensional-array/141633/6 "2008-06-30T01:57:41Z")

</div>

On Jun 27, 2008, at 4:23 PM, Justin To wrote:

> end
> 
> if(lh[0]==rh[0])  
> end
> 
> end
> 
> ## This is what I’ve got so far, but I can’t seem to figure out how to write methods for: copying a Tda to another Tda, and indexing the Tda. Thanks for the help!
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

gem install narray

it’s this and _much_ _much_ more - not to mention insanely fast.

> 

a @ [http://codeforpeople.com/](http://codeforpeople.com/)
