Forum: Ruby Newbie question (Array.<<)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8d50470327c9f59b275f532026b465ec?d=identicon&s=25 Todd Breiholz (Guest)
on 2006-01-08 04:17
(Received via mailing list)
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)
        p objs
      end
      objs
    end

Thanks!

Todd Breiholz
C2cd72c24873556e278b44b5b3c7ef33?d=identicon&s=25 angus (Guest)
on 2006-01-08 05:53
(Received via mailing list)
[Todd Breiholz <talanb@gmail.com>, 2006-01-08 04.15 CET]
> [00630000003BqXwAAK]
>       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.
C2cd72c24873556e278b44b5b3c7ef33?d=identicon&s=25 angus (Guest)
on 2006-01-08 05:56
(Received via mailing list)
[Todd Breiholz <talanb@gmail.com>, 2006-01-08 04.15 CET]
> [00630000003BqXwAAK]
>       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.
This topic is locked and can not be replied to.