From: Austin Ziegler Date: 2004-09-03T04:27:28+09:00 Subject: Re: array of objects within an object On Fri, 3 Sep 2004 04:05:19 +0900, DiesIrae wrote: > I am storing an array of objects within another object, but am not > sure how to print the values of the array out. When I do an > Port.inspect, it gives me something like this: [minor editing for clarity] > # @state=[#, > # ], > @protocol="tcp", > @owner=[#, > #> > > But when I do a Port.print_port, defined as class Port > def print_port > "Port: #{@protocol}, #{@state}, #{@owner} " > end end Right. What you're doing here is implicitly calling #to_s on each of your values through the use of interpolation. Thus, you have two issues: 1. Neither State or Owner define #to_s values. This will harm readability. However, even if you do: class State def to_s "State: #{@state}" end end class Owner def to_s "Owner: #{@name}" end end you'll end up getting: Port: tcp, State: 1State: 2, Owner: 11Owner: 12 Thus, you need to change your Port #to_s and #print_port definition to: class Port def print_port to_s end def to_s %Q|Port: #{@protocol}, [#{@state.join(", ")}], [#{@owner.join(", ")}]| end end With this, you'll get: Port: tcp, [State: 1, State: 2], [Owner: 11, Owner: 12] -a -- Austin Ziegler * halostatue@gmail.com * Alternate: austin@halostatue.ca : as of this email, I have [ 6 ] Gmail invitations