Mike Jo wrote:
> The problem is with #add_collected. When it is called its updating
> @collected for all instances of Node. @collected is suppose to be an
> array of arrays. I'm not sure why it's behaving the way that it is. I'm
> still a bit new to ruby. Can anyone give me some insight on why its
> behaving the way that it is please. Thanks.
1. The method << is an in-place modifier, you don't have to assign the
result, you just write array<<elem and it adds elem to array.
2. If @collected is going to be an array of two-element arrays, then you
should write @collected<<[@row,@col] - only one pair of square brackets.
This is because the << method treats the object on the right as an
element and does not check if it is or is not an array. Check it
yourself in irb:
irb(main):010:0> a=[]
=> []
irb(main):011:0> a<<[1,2]
=> [[1, 2]]
irb(main):012:0> a<<[2,5]
=> [[1, 2], [2, 5]]
3. My guess for your main problem is that you create just one instance
of Node and then assign it to all the places where you want to keep your
nodes. If you create your nodes like this:
nodes=Array::new(num_nodes,Node::new(args)) then this is the case - you
create one node and populate the array with pointers to the node. (The
correct solution would be nodes=Array::new(num_nodes){Node::new(args)}
because this form calls the block for each newly created elements, so
you'd have num_nodes separate nodes.) But it's just my guess, you'd have
to post your code that creates the nodes to verify it.
TPR.
--
Posted via
http://www.ruby-forum.com/.