Array of objects within an object

Hi all,

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:

#<Port:0x4021de58 @state=[#<State:0x4021dca0 @state="1">,
#<State:0x4021d8f4 @state="2">], @protocol="tcp",
@owner=[#<Owner:0x4021d9a8 @name="11">, #<Owner:0x4021d854
@name="12">>

But when I do a Port.print_port, defined as

  def print_port
    "Port: #{@protocol}, #{@state}, #{@owner} "
  end

it prints:

Port: tcp, #<State:0x4021dcc8>#<State:0x4021d8cc>,
#<Owner:0x4021d9d0>#<Owner:0x4021d82c>

So, How do I address the object with the ID of #<State:0x4021dcc8>,
and print out its constituent values?

thanks for any help.

Dan

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]

#<Port:0x4021de58
  @state=[#<State:0x4021dca0 @state="1">,
          #<State:0x4021d8f4 @state="2"> ],
  @protocol="tcp",
  @owner=[#<Owner:0x4021d9a8 @name="11">,
          #<Owner:0x4021d854 @name="12">>

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

···

On Fri, 3 Sep 2004 04:05:19 +0900, DiesIrae <danb@mojolin.com> wrote:
--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca
: as of this email, I have [ 6 ] Gmail invitations

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...:

If you are dealing with modest sized, non-recursive structures, you can
throw together something like this:

class Object
    def pretty(indent = '')
        if instance_variables.length > 0
            self.class.to_s+":\n"+(instance_variables.collect { |v|
                indent+v+':
'+instance_variable_get(v).pretty(indent+' ')
                }.join("\n"))
          else
            self.class.to_s
          end
        end
    end

class Numeric
    def pretty(indent = '')
        to_s
        end
    end

class String
    def pretty(indent = '')
        inspect
        end
    end

class Array
    def pretty(indent = '')
        super + "[\n"+
            collect{ |v|
                ' '+indent+v.pretty(indent+' ')
                }.join(",\n") + "\n"+
            " " + indent+"]"
        end
    end

class My_class
    attr_accessor :test,:aa
    def initialize
        @test = "goo"
        @aa = [nil,true,8]
        end
    end

print ["test",7,My_class.new].pretty,"\n"

Not "production quality" but quick & easy for debugging purposes, and
easy to extend (and, to some extent, self-extending).

-- MarkusQ

Hello,

I think you need to define to_s methods for your
Port, State and Owner classes. These to_s methods
should return a string representation of the class
they belong to (e.g. the state of the current object's
instance variables). The Object#inspect method will
then call these methods when invoked, instead of
calling Object#to_s

hope this helps,

Brian

···

--- DiesIrae <danb@mojolin.com> wrote:

Hi all,

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:

#<Port:0x4021de58 @state=[#<State:0x4021dca0
@state="1">,
#<State:0x4021d8f4 @state="2">], @protocol="tcp",
@owner=[#<Owner:0x4021d9a8 @name="11">,
#<Owner:0x4021d854
@name="12">>

But when I do a Port.print_port, defined as

  def print_port
    "Port: #{@protocol}, #{@state}, #{@owner} "
  end

it prints:

Port: tcp, #<State:0x4021dcc8>#<State:0x4021d8cc>,
#<Owner:0x4021d9d0>#<Owner:0x4021d82c>

So, How do I address the object with the ID of
#<State:0x4021dcc8>,
and print out its constituent values?

thanks for any help.

Dan

__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - 100MB free storage!
http://promotions.yahoo.com/new_mail