How to print out all of an object's instance variables?

J

Jian Lin

When it is RoR, we can use ActiveRecord's attributes method to get a
hash for a record's column name and values. How about if it is just an
object in Ruby, and we want to print out all instance variables to
debug, while the inspect() method doesn't print everything out, is there
a way to print out all instance variables (for any object)
automatically, for example, to debug the code? thanks.
 
J

Jian Lin

Dhruva said:
require 'pp'
pp object_reference


hm, looks like inspect() by default will print out all the instance
variables...

but what if not using inspect()? such as wanting to print them out in a
table format, between <td> and </td> tags?

require 'pp' works until inspect is redefined, then it will stop working
and give the following error. thanks for giving out starting pointers
though.

irb(main):009:0> class Point
irb(main):010:1> def initialize(x,y)
irb(main):011:2> @x,@y = x,y
irb(main):012:2> @something = 1
irb(main):013:2> end
irb(main):014:1> end
=> nil
irb(main):015:0> p = Point.new(1,2)
=> #<Point:0x253f260 @y=2, @x=1, @something=1>

irb(main):016:0> p p
#<Point:0x253f260 @y=2, @x=1, @something=1>
=> nil

irb(main):017:0> class Point
irb(main):018:1> def inspect
irb(main):019:2> p @x, @y
irb(main):020:2> end
irb(main):021:1> end
=> nil
irb(main):022:0> p
1
2
=>
irb(main):023:0> p p
1
2

=> nil
irb(main):024:0> require 'pp'
=> true
irb(main):025:0> pp p
1
2
NoMethodError: undefined method `length' for nil:NilClass
from c:/Ruby/lib/ruby/1.8/prettyprint.rb:144:in `text'
from c:/Ruby/lib/ruby/1.8/pp.rb:259:in `pretty_print'
from c:/Ruby/lib/ruby/1.8/pp.rb:140:in `pp'
from c:/Ruby/lib/ruby/1.8/prettyprint.rb:201:in `group'
from c:/Ruby/lib/ruby/1.8/prettyprint.rb:227:in `nest'
from c:/Ruby/lib/ruby/1.8/prettyprint.rb:200:in `group'
from c:/Ruby/lib/ruby/1.8/prettyprint.rb:212:in `group_sub'
from c:/Ruby/lib/ruby/1.8/prettyprint.rb:199:in `group'
from c:/Ruby/lib/ruby/1.8/pp.rb:140:in `pp'
from c:/Ruby/lib/ruby/1.8/pp.rb:77:in `pp'
from c:/Ruby/lib/ruby/1.8/pp.rb:119:in `guard_inspect_key'
from c:/Ruby/lib/ruby/1.8/pp.rb:77:in `pp'
from c:/Ruby/lib/ruby/1.8/pp.rb:60:in `pp'
from c:/Ruby/lib/ruby/1.8/pp.rb:59:in `each'
from c:/Ruby/lib/ruby/1.8/pp.rb:59:in `pp'
from (irb):25
irb(main):026:0>
 
J

Jian Lin

Jian said:
require 'pp' works until inspect is redefined, then it will stop working
and give the following error. thanks for giving out starting pointers
though.

on the other hand, if inspect() is to return a string representation,
then 'pp' still works except it doesn't print out all the instance
variables:

irb(main):026:0> class Point
irb(main):027:1> def inspect
irb(main):028:2> "#@x #@y and i am a point"
irb(main):029:2> end
irb(main):030:1> end
=> nil
irb(main):031:0> p
=> 1 2 and i am a point

irb(main):032:0> p p
1 2 and i am a point
=> nil

irb(main):033:0> pp p # note that @something is not printed
out
1 2 and i am a point
=> nil

irb(main):034:0>
 
C

Caleb Clausen

hm, looks like inspect() by default will print out all the instance
variables...

but what if not using inspect()? such as wanting to print them out in a
table format, between <td> and </td> tags?

require 'pp' works until inspect is redefined, then it will stop working
and give the following error. thanks for giving out starting pointers
though.

irb(main):009:0> class Point
irb(main):010:1> def initialize(x,y)
irb(main):011:2> @x,@y = x,y
irb(main):012:2> @something = 1
irb(main):013:2> end
irb(main):014:1> end
=> nil
irb(main):015:0> p = Point.new(1,2)
=> #<Point:0x253f260 @y=2, @x=1, @something=1>

irb(main):016:0> p p
#<Point:0x253f260 @y=2, @x=1, @something=1>
=> nil

irb(main):017:0> class Point
irb(main):018:1> def inspect
irb(main):019:2> p @x, @y
irb(main):020:2> end
irb(main):021:1> end
=> nil
irb(main):022:0> p
1
2
=>
irb(main):023:0> p p
1
2

=> nil
irb(main):024:0> require 'pp'
=> true
irb(main):025:0> pp p
1
2
NoMethodError: undefined method `length' for nil:NilClass
from c:/Ruby/lib/ruby/1.8/prettyprint.rb:144:in `text'
from c:/Ruby/lib/ruby/1.8/pp.rb:259:in `pretty_print'
from c:/Ruby/lib/ruby/1.8/pp.rb:140:in `pp'
from c:/Ruby/lib/ruby/1.8/prettyprint.rb:201:in `group'
from c:/Ruby/lib/ruby/1.8/prettyprint.rb:227:in `nest'
from c:/Ruby/lib/ruby/1.8/prettyprint.rb:200:in `group'
from c:/Ruby/lib/ruby/1.8/prettyprint.rb:212:in `group_sub'
from c:/Ruby/lib/ruby/1.8/prettyprint.rb:199:in `group'
from c:/Ruby/lib/ruby/1.8/pp.rb:140:in `pp'
from c:/Ruby/lib/ruby/1.8/pp.rb:77:in `pp'
from c:/Ruby/lib/ruby/1.8/pp.rb:119:in `guard_inspect_key'
from c:/Ruby/lib/ruby/1.8/pp.rb:77:in `pp'
from c:/Ruby/lib/ruby/1.8/pp.rb:60:in `pp'
from c:/Ruby/lib/ruby/1.8/pp.rb:59:in `each'
from c:/Ruby/lib/ruby/1.8/pp.rb:59:in `pp'
from (irb):25
irb(main):026:0>

Your definition of inspect is bad. inspect should just return a
string; it shouldn't try to print out anything itself.

If you just want to get back to the original definition of inspect in
a class that has redefined it, use this trick:

Object.instance_method:)inspect).bind(x).call

where x is the variable that you want to inspect.
 
G

Gabriel Horner

Jian said:
When it is RoR, we can use ActiveRecord's attributes method to get a
hash for a record's column name and values. How about if it is just an
object in Ruby, and we want to print out all instance variables to
debug, while the inspect() method doesn't print everything out, is there
a way to print out all instance variables (for any object)
automatically, for example, to debug the code? thanks.

I just use the following irb command:
def instance_var(obj)
obj.instance_variables.map {|e| [e, obj.instance_variable_get(e)] }
end
If you define the irb command as a boson command, you get a nice ascii
table of an object's instance variables:
http://tagaholic.me/2009/11/07/ruby-reference-commands-with-boson.html#object_instance_variables
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top