Accessing child variables

#!/usr/bin/env ruby-1.9.2

class A
attr_accessor :valA
end

class B
attr_accessor :myarray

def initialize
  @myarray = []
end

def myarray
  @myarray = myarray
end

end #end Class

class My_top

attr_accessor :c

objA = A.new
objA.valA = 10

c = B.new
c.myarray << objA

end

my_top = My_top.new
puts my_top.c.myarray.inspect

From this is get:

stack level too deep (SystemStackError)

Can anyone help?

you forgot the def initialize around your line with objA

Hans M. wrote in post #1060677:

you forgot the def initialize around your line with objA

Hi

Thanks but that fix makes no difference. I have something more
fundamentally wrong here but I’m not sure what.

My error is as follows:

stack level too deep (SystemStackError)

This is a hint to you that a method you are calling is calling itself.

If you look at your implementation of myarray, you have “@myarray =
myarray”. This means that when you call myarray, it’s calling itself
over and over again. This is recursion and in this situation, this is
bad.

At the top of your class you have “attr_accessor :myarray”

That in effect defines the following two methods:
def myarray
return @myarray
end

def myarray=(arr)
@myarray = arr
end

Your method of “myarray” is overriding the default one and causing the
problem. Try removing it and see if that fixes it.

Jeremy W.

Sm sm wrote in post #1060682:

Hans M. wrote in post #1060677:

you forgot the def initialize around your line with objA

Hi

Thanks but that fix makes no difference. I have something more
fundamentally wrong here but I’m not sure what.

Actually my last line is incorrect

was: puts my_top.c.inspect
should be : my_top.c.myarray.inspect

This still errors with the same issue!

I would recommend reading a good book on Ruby to understand how
attr_accessor and instance variables work. I’d also use more descriptive
names when you’re trying to learn these concepts. It’s much easier to
program when thinking in terms of Cars containing Wheels with Tyres,
that it is A with a B, and a objC etc.

Search if a good place to
start.

The code below works. Good luck!

class A
attr_accessor :valA
end

class B
attr_accessor :myarray

def initialize
@myarray = []
end
end

class My_top
attr_accessor :c

def initialize
objA = A.new
objA.valA = 10

@c = B.new
@c.myarray << objA

end
end

my_top = My_top.new
puts my_top.c.myarray.inspect

Jeremy W. wrote in post #1060687:

I would recommend reading a good book on Ruby to understand how
attr_accessor and instance variables work. I’d also use more descriptive
names when you’re trying to learn these concepts. It’s much easier to
program when thinking in terms of Cars containing Wheels with Tyres,
that it is A with a B, and a objC etc.

Search if a good place to
start.

The code below works. Good luck!

class A
attr_accessor :valA
end

class B
attr_accessor :myarray

def initialize
@myarray = []
end
end

class My_top
attr_accessor :c

def initialize
objA = A.new
objA.valA = 10

@c = B.new
@c.myarray << objA

end
end

my_top = My_top.new
puts my_top.c.myarray.inspect

Hi Jeremy

Thats perfect. Thanks for your help. I will read through the suggested
book.

Many Thanks

Jeremy W. wrote in post #1060683:

My error is as follows:

stack level too deep (SystemStackError)

This is a hint to you that a method you are calling is calling itself.

If you look at your implementation of myarray, you have “@myarray =
myarray”. This means that when you call myarray, it’s calling itself
over and over again. This is recursion and in this situation, this is
bad.

At the top of your class you have “attr_accessor :myarray”

That in effect defines the following two methods:
def myarray
return @myarray
end

def myarray=(arr)
@myarray = arr
end

Your method of “myarray” is overriding the default one and causing the
problem. Try removing it and see if that fixes it.

Jeremy W.

Hi Jeremy

Thanks for the tip. I understand my error and I believe it is the same
for my “c” method as it is also included in the attr_accessor. I have
removed these and I now have the following:

“undefined method `myarray’ for nil:NilClass (NoMethodError)”

This error is referring to puts my_top.c.myarray.inspect

Any ideas? Do I have a scoping issue here?

Hi,

By the way, the correct term is “instance variable”, not “child
variable”.