d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Any Way To Create A 2d Array In Ruby?
Add Reply New Topic New Poll
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Jun 6 2013 08:58am
Pretty much I have grid points (0,0) (0,1)...ect

I want to create a cell object that has an x coordinate, y coordinate, and a movement direction (defined by left, right, up, down) telling where you can move

I'm confused on how to create an appropriate structure for it though

Do I create an array of Cell objects? If so how..? Not familiar with ruby syntax at all.
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Jun 6 2013 09:34am
Ok trying to clarify this post, I pretty much want to have something like this:

Code

class Cell
   def initialize(x,y)
     @flag = "na"
     @directions = Hash.new
     @x = x
     @y = y
   end
   def set_flag(str)
      @flag = str
   end
end

@this[x.to_i][y.to_i] = Cell.new(x.to_i, y.to_i)
@this[x.to_i][y.to_i].set_flag("start")

@this[ex.to_i][ey.to_i] = Cell.new(ex.to_i, ey.to_i)
@this[ex.to_i][ey.to_i].set_flag("end")

#and then make other cells that aren't the start or ending cells

Member
Posts: 50,343
Joined: Apr 3 2008
Gold: 0.00
Jun 6 2013 11:08am
From abduct:

currently suspended so bear that in mind.

anyways i am not entirely sure what you are trying to acomplish and i suggest simply opening up IRB and running some simple tests.

also there is a structure to variables in ruby and i am unsure why you are setting "this" to an instance variable.

if you havent figured out variables are like so,

Code

this, that, something       |are all normal variables
:this, :that, :something    |are all string symbols
This, That, Something       |are all constant variables
$this, $that, $something    |are all global variables
@this, @that, @something    |are all instence variables
@@this, @@that, @@something |are all global class variables


difference between @ and @@ is that @ is a variable pertaining to that class that was initialized, while @@ variables can be accessed by any class of that type which is initiliazed.

as for your array question there are different ways to handle this.

first way which is possibly the easiest is to create an array and then push your datasets to the array as a array.


Code

test = []
test.push [x.to_i, y.to_i]
puts test[0][0]
puts test[0][1]


next would be creating an array with predetermaned number of elements

Code

test = Array.new(10) { Array.new(2) }
=> [[nil, nil], [nil, nil], [nil, nil], [nil, nil], [nil, nil], [nil, nil], [nil, nil], [nil, nil], [nil, nil], [nil, nil]]


personally i would create a single dimontional array and push arrays into the exisisting array, much easier to manage then working with a prebuilt array its dynamically built and you only add as many elements as you need. further you can still use push, pop, shift methods to add to the end, take off the end, or take off the front of the array.

This post was edited by Rikuo on Jun 6 2013 11:09am
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Jun 6 2013 03:13pm
Yea I actually think I figured it out, something like:

Code
arrayT = Array.new(size_t){ Array.new(size_t) }


Creates a 2d array like: arrayT[x][y]

This post was edited by lopelurag on Jun 6 2013 03:13pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll