Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Instance variable acting like class variable?

Reply
Thread Tools

Instance variable acting like class variable?

 
 
Mike Mr.
Guest
Posts: n/a
 
      09-21-2008
I've written a Node class that I'm using to write a few programs for in
a course.

class Node
# A node represents a spot on the grid. It knows its
# location and parent node, the total cost so far, and
# the directions take to get to current location
attr_reader :row, :col, :cost, :direction, arent, :collected

def initialize(row, col, cost, direction=nil, parent=nil)
@row = row
@col = col
@cost = (parent.nil? ? 0.0 : parent.cost) + cost.to_i
@direction = (parent.nil?) ? "" : parent.direction + direction
@parent = parent
@collected = (parent.nil?) ? [] : parent.collected.to_a
end

def to_s
"row:#{@row} col:#{@col} cost:#{@cost}"
end

def location
[@row, @col]
end

def add_collected
@collected = @collected << [[@row, @col]]
end
end

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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Thomas B.
Guest
Posts: n/a
 
      09-21-2008
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/.

 
Reply With Quote
 
 
 
 
Mike Jo
Guest
Posts: n/a
 
      09-21-2008
Thomas B. wrote:
> 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.


I do have an array of Node objects, but I'm calling Node.new(args) each
times I add a node to the array.

Example:

@nodes = Array.new
@nodes << Node.new(args)
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Rick DeNatale
Guest
Posts: n/a
 
      09-21-2008
[Note: parts of this message were removed to make it a legal post.]

On Sun, Sep 21, 2008 at 3:47 PM, Mike Mr. <(E-Mail Removed)> wrote:

> I've written a Node class that I'm using to write a few programs for in
> a course.
>
> class Node
> #...
>
> def initialize(row, col, cost, direction=nil, parent=nil)
> #...


@collected = (parent.nil?) ? [] : parent.collected.to_a
> end
>
>
> The problem is with #add_collected. When it is called its updating
> @collected for all instances of Node.



You are confusing the instance variable @collected with the object it
references.

Every instance of Node will have it's own instance variable, BUT

assume that you do

n1 = Node.new(r1, c1, cost1, direction1)
n2 = Node.new(r2, c2, cost2, direction2, n1)

Now since Array#to_a returns self (i.e. the same array), n2.collected will
refer to exactly the same array as n1.collected, and so any changes to the
object will be visible through either instance variable.

If you change the parent.collected.to_a to parent_collected.dup then it will
break this alias by making a shallow copy of the parents array at the time
of initialization, but, not knowing what you are trying to do, this might or
might not fix your actual problem.


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

 
Reply With Quote
 
Mike Jo
Guest
Posts: n/a
 
      09-21-2008
Rick Denatale wrote:


> Every instance of Node will have it's own instance variable, BUT
>
> assume that you do
>
> n1 = Node.new(r1, c1, cost1, direction1)
> n2 = Node.new(r2, c2, cost2, direction2, n1)
>
> Now since Array#to_a returns self (i.e. the same array), n2.collected
> will
> refer to exactly the same array as n1.collected, and so any changes to
> the
> object will be visible through either instance variable.
>
> If you change the parent.collected.to_a to parent_collected.dup then it
> will
> break this alias by making a shallow copy of the parents array at the
> time
> of initialization, but, not knowing what you are trying to do, this
> might or
> might not fix your actual problem.
>
> --
> Rick DeNatale


Rick,
That was exactly what I needed. Thank you!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Don't understand behavior; instance form a class in another class'instance Martin P. Hellwig Python 1 03-26-2010 12:06 AM
Problem when subclass instance changes base class instance variable Gerry Sutton Python 1 04-16-2005 06:06 AM
accessing class instance variable from instance method David Garamond Ruby 5 06-08-2004 02:26 PM
converting base class instance to derived class instance Sridhar R Python 14 02-10-2004 02:47 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM



Advertisments