why one array continues to grow after repeated call

B

Brian Candler

Li said:
The script in the OP is a simple version of another more lenghy one.

@a1 comprise all the file names from a folder.
@b1 comprise all the file names from another folder.
the construction of @b1 is dependent on @a1.
after initialize I want to check the status of @a1 and @b1.
out of curiossity I call both method1 and method2 more than once.

So if the idea is for method1 to initialize @a1, and for method2 to
initialize @b1, then do it like this:

def initialize
method1
method2
end
def method1
@a1 = [1,2,3,4]
end
def method2
@b1 = []
@a1.size.times { @b1 << 10 }
@b1
end
I think I can retrieve the status of @a1 and @b1 by
writing accessor/getter. This way I would not mess up with the method
call.

Sure:

def a1
@a1
end
def b1
@b1
end

or more simply:
attr_reader :a1, :b1

If you want, you can make the accessor method also perform the
initialization. Here's a common pattern:

def a1
@a1 ||= method1
end
def b1
@b1 ||= method2
end

B.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top