Two Dimensional Array

Discussion in 'Ruby' started by Justin To, Jun 27, 2008.

  1. Justin To

    Justin To Guest

    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!
    --
    Posted via http://www.ruby-forum.com/.
    Justin To, Jun 27, 2008
    #1
    1. Advertising

  2. 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.each_index { |j| new_a <<
    original_array[j].clone }
    new_tda << new_a
    end
    return new_tda
    end
    --
    Posted via http://www.ruby-forum.com/.
    Shashank Agarwal, Jun 29, 2008
    #2
    1. Advertising

  3. Justin To

    Todd Benson Guest

    On Fri, Jun 27, 2008 at 5:23 PM, Justin To <> 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
    Todd Benson, Jun 29, 2008
    #3
  4. Justin To

    ara.t.howard Guest

    On Jun 27, 2008, at 4:23 PM, 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!
    > --
    > Posted via http://www.ruby-forum.com/.



    gem install narray

    it's this and *much* *much* more - not to mention insanely fast.


    >
    >


    a @ http://codeforpeople.com/
    --
    we can deny everything, except that we have the possibility of being
    better. simply reflect on that.
    h.h. the 14th dalai lama
    ara.t.howard, Jun 30, 2008
    #4
  5. Hi --

    On Sat, 28 Jun 2008, Justin To wrote:

    > #--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


    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

    --
    Rails training from David A. Black and Ruby Power and Light:
    ADVANCING WITH RAILS July 21-24 Edison, NJ
    See http://www.rubypal.com for details and updates!
    David A. Black, Jun 30, 2008
    #5
  6. Justin To

    Justin To Guest

    Justin To, Jun 30, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alf P. Steinbach
    Replies:
    0
    Views:
    423
    Alf P. Steinbach
    Aug 18, 2003
  2. John Harrison
    Replies:
    4
    Views:
    6,914
    Default User
    Aug 19, 2003
  3. Icosahedron
    Replies:
    8
    Views:
    643
    Vivek
    Aug 21, 2003
  4. Venkat
    Replies:
    4
    Views:
    963
    Venkat
    Dec 5, 2003
  5. Kenneth Brody

    Two-dimensional array to array of arrays

    Kenneth Brody, Dec 21, 2006, in forum: C Programming
    Replies:
    2
    Views:
    497
    Simon Biber
    Dec 21, 2006
Loading...

Share This Page