Dynamically read object's attributes

I want to read the values of instance variables in an object, but I
don't know how to reach them. I think the code should be something
like:

def read_stuff
        s = "Class: #{self.class}:\n"
        s += "Instance variables:\n"
        self.instance_variables.each do
                >var>
                s += "#{var}: #{SOMETHING}\n"
        end
        s += "\n"
end

But I don't know what to replace SOMETHING with. Anyone know?

···

--
Posted via http://www.ruby-forum.com/.

Alle venerdì 21 settembre 2007, Mark Pelletier ha scritto:

I want to read the values of instance variables in an object, but I
don't know how to reach them. I think the code should be something
like:

def read_stuff
        s = "Class: #{self.class}:\n"
        s += "Instance variables:\n"
        self.instance_variables.each do

                >var>

                s += "#{var}: #{SOMETHING}\n"
        end
        s += "\n"
end

But I don't know what to replace SOMETHING with. Anyone know?

instance_variable_get var

Stefano

Mark Pelletier wrote:

I want to read the values of instance variables in an object, but I
don't know how to reach them. I think the code should be something
like:

def read_stuff
        s = "Class: #{self.class}:\n"
        s += "Instance variables:\n"
        self.instance_variables.each do
                >var>
                s += "#{var}: #{SOMETHING}\n"
        end
        s += "\n"
end

But I don't know what to replace SOMETHING with. Anyone know?

Object#instance_variable_get

···

--
Posted via http://www.ruby-forum.com/\.

instance_variable_get(var)

You might want to read rdoc for Object and Module for more similar
interesting methods.

···

On 9/21/07, Mark Pelletier <mark.pelletier@asrcms.com> wrote:

I want to read the values of instance variables in an object, but I
don't know how to reach them. I think the code should be something
like:

def read_stuff
        s = "Class: #{self.class}:\n"
        s += "Instance variables:\n"
        self.instance_variables.each do
                >var>
                s += "#{var}: #{SOMETHING}\n"
        end
        s += "\n"
end

But I don't know what to replace SOMETHING with. Anyone know?

Thanks to both of you.

Mark

Stefano Crocco wrote:

···

Alle venerdì 21 settembre 2007, Mark Pelletier ha scritto:

                s += "#{var}: #{SOMETHING}\n"
        end
        s += "\n"
end

But I don't know what to replace SOMETHING with. Anyone know?

instance_variable_get var

Stefano

--
Posted via http://www.ruby-forum.com/\.