Hello,

Short question:

How to make an iterative method of stack?

Long explanation:
I'm trying to create an iterative method for following structure:

class Obj
   attr_accessor: id,: parent_id

   def initialize (id, parent_id)
     parent_id =3D @ parent_id
     @ id =3D id
   end

   def to_s
     "id: # {@ id}, parent_id: @ # {parent_id} '
   end
end

left =3D [Obj.new (1.0), Obj.new (2.1), Obj.new (3.2), Obj.new (4.0),
Obj.new (5.1), Obj new (6.1), Obj.new (7.3)]

My challenge is to make an iterative method, see how far I got:

def menu_iterative (menu)
   res =3D []

   el1 =3D menu.shift
   res << el1
   while not menu.empty?
     res_aux =3D []
     for i in menu
       if el1.id =3D=3D i.parent_id
         print el1, "=3D=3D" i "* ", res_aux.index (el1), "\ n"
         res_aux << i
         el1 =3D menu.delete (i)
         retry
       else
         print el1, "! =3D" i, "\ n"
       end
     end
     res << res_aux unless res_aux.empty?
     puts
     el1 =3D menu.shift
     res << el1
   end
   return res
end

puts "result:"
pp menu_iterative (left_it)

I wish the outcome was:

[#<Obj:0x7f35b48cab40 @id=3D1, @parent_id=3D0>,
 [#<Obj:0x7f35b48cab18 @id=3D2, @parent_id=3D1>,
  [#<Obj:0x7f35b48caaf0 @id=3D3, @parent_id=3D2>,
   [#<Obj:0x7f35b48caa50 @id=3D7, @parent_id=3D3>]],
  [#<Obj:0x7f35b48caaf0 @id=3D3, @parent_id=3D2>]],
 [#<Obj:0x7f35b48cab18 @id=3D2, @parent_id=3D1>,
  #<Obj:0x7f35b48caaa0 @id=3D5, @parent_id=3D1>,
  #<Obj:0x7f35b48caa78 @id=3D6, @parent_id=3D1>]]

It seems that the problem is attribution without stack, correct?
I'm attaching all I did.

Thanks for any help.
--=20
Marcello Henrique
Blog - http://faraohh.wordpress.com
Associa=E7=E3o Software Livre de Goi=E1s (www.aslgo.org.br)
Cercomp - UFG (www.cercomp.ufg.br)
IyEvdXNyL2Jpbi9ydWJ5CnJlcXVpcmUgJ3BwJwoKIyBDbGFzc2VzIHRvIHNpbXVsYXRlIG9iamVj
dHMKY2xh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