display instance variable values

J

John Sayeau

If I have a people class with instance variables @name @age and @gender
and an instance of that class called peep (justa an example).
If I want to see all the available instance variables I can do:

p peep.instance_variables => ["@age", "@name", "@gender"]

Is there a way to get the value of those instance variables for the peep
instance without calling a class method ? Say there is full r/w access
to the instance variables.

Something like:


peep.instance_variables.each do |var|

var.(display value somehow)

end



Thanks.
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

If I have a people class with instance variables @name @age and @gender
and an instance of that class called peep (justa an example).
If I want to see all the available instance variables I can do:

p peep.instance_variables => ["@age", "@name", "@gender"]

Is there a way to get the value of those instance variables for the peep
instance without calling a class method ? Say there is full r/w access
to the instance variables.

Something like:


peep.instance_variables.each do |var|

var.(display value somehow)

end



Thanks.
replace the line "var.(display value somehow)" with "puts
peep.instance_variable_get(var)"
 
A

Ammar Ali

If I have a people class with instance variables @name @age and @gender
and an instance of that class called peep (justa an example).
If I want to see all the available instance variables I can do:

p peep.instance_variables =3D> ["@age", "@name", "@gender"]

Is there a way to get the value of those instance variables for the peep
instance without calling a class method ? Say there is full r/w access
to the instance variables.

Something like:


peep.instance_variables.each do |var|

=C2=A0 var.(display value somehow)

end

What are you trying to do? This seems a little strange to me.

In any case, if the instance variable are readable or accessible, i.e.
defined with attr_reader, attr_accessor, or have methods to get them,
you can use send to get at their values:

peep.instance_variables.each do |var|
puts peep.send(var.sub('@', ''))
end

Again, it feels a little strange to me.

HTH,
Ammar
 
M

Michael Bostler

Try using instance_variable_get:

p peep.instance_variables # => ["@age", "@name", "@gender"]

peep.instance_variables.each do |var|
p var.instance_variable_get var
end
 
J

John Sayeau

That won't work.
Your asking an instance variable (var) to show its instance variables.
Tried it just to be sure.
 
J

John Sayeau

Not trying to do anything other than experiment. I was playing with for
the first time today and the book I'm readin said the view had access to
the controller's instance variables. I was trying to find out what other
instance variables/values the view could access. in the erb file id put
this code in:
<% self.instance_variables.each do |var| %>
<%= h(var) %>
<%end%>

And it gave me a list of the instance variables without values... just
trying to learn by experimenting.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top