Newbie question (Array.<<)

T

Todd Breiholz

------=_Part_44639_21578459.1136690126909
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

I must be missing something...

I've got the following class method. The problem is the line that reads obj=
s
<< self.new(obj). The first time through the loop, the objs array looks
correct. After the second iteration however, the objs array contains 2
copies of the second object created.

Here is the output from the method:

"00630000003BqXwAAK"
[00630000003BqXwAAK]
"00630000003awD3AAI"
[00630000003awD3AAI, 00630000003awD3AAI]

and here is the method:

def FindAll(criteria)
self.GetAllFields if (defined? @@attribs) =3D=3D nil
result =3D @@connection.query([:query, "select #{@@attribs} from #{
self.to_s} where #{criteria}"])
objs =3D []
result.queryResponse.result.records.each do |obj|
p obj.Id
objs << self.new(obj)
p objs
end
objs
end

Thanks!

Todd Breiholz

------=_Part_44639_21578459.1136690126909--
 
A

angus

I must be missing something...

I've got the following class method. The problem is the line that reads objs
<< self.new(obj). The first time through the loop, the objs array looks
correct. After the second iteration however, the objs array contains 2
copies of the second object created.

Here is the output from the method:

"00630000003BqXwAAK"
[00630000003BqXwAAK]
"00630000003awD3AAI"
[00630000003awD3AAI, 00630000003awD3AAI]

and here is the method:

def FindAll(criteria)
self.GetAllFields if (defined? @@attribs) == nil
result = @@connection.query([:query, "select #{@@attribs} from #{
self.to_s} where #{criteria}"])
objs = []
result.queryResponse.result.records.each do |obj|
Probably this object is being reused ----------------^^^

Look at the documentation (or source code) of the method that does the
iteration.

Good luck.
 
A

angus

I must be missing something...

I've got the following class method. The problem is the line that reads objs
<< self.new(obj). The first time through the loop, the objs array looks
correct. After the second iteration however, the objs array contains 2
copies of the second object created.

Here is the output from the method:

"00630000003BqXwAAK"
[00630000003BqXwAAK]
"00630000003awD3AAI"
[00630000003awD3AAI, 00630000003awD3AAI]

and here is the method:

def FindAll(criteria)
self.GetAllFields if (defined? @@attribs) == nil
result = @@connection.query([:query, "select #{@@attribs} from #{
self.to_s} where #{criteria}"])
objs = []
result.queryResponse.result.records.each do |obj|
p obj.Id
objs << self.new(obj)
Or, more probably ^^^^^^^^ here is the problem :). Try self.class.new if you
want a new object of the same class as self.
 

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top