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

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.

require ‘pp’
pp object_reference

On Wed, May 19, 2010 at 14:10, Jian L. [email protected] wrote:

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.

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

Thanks & Regards,
Dhruva S…

Dhruva S. wrote:

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

and 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:intext’
from c:/Ruby/lib/ruby/1.8/pp.rb:259:in pretty_print' from c:/Ruby/lib/ruby/1.8/pp.rb:140:inpp’
from c:/Ruby/lib/ruby/1.8/prettyprint.rb:201:in group' from c:/Ruby/lib/ruby/1.8/prettyprint.rb:227:innest’
from c:/Ruby/lib/ruby/1.8/prettyprint.rb:200:in group' from c:/Ruby/lib/ruby/1.8/prettyprint.rb:212:ingroup_sub’
from c:/Ruby/lib/ruby/1.8/prettyprint.rb:199:in group' from c:/Ruby/lib/ruby/1.8/pp.rb:140:inpp’
from c:/Ruby/lib/ruby/1.8/pp.rb:77:in pp' from c:/Ruby/lib/ruby/1.8/pp.rb:119:inguard_inspect_key’
from c:/Ruby/lib/ruby/1.8/pp.rb:77:in pp' from c:/Ruby/lib/ruby/1.8/pp.rb:60:inpp’
from c:/Ruby/lib/ruby/1.8/pp.rb:59:in each' from c:/Ruby/lib/ruby/1.8/pp.rb:59:inpp’
from (irb):25
irb(main):026:0>

Jian L. wrote:

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>

On 5/19/10, Jian L. [email protected] wrote:

=> nil
irb(main):020:2> end
=> nil
from c:/Ruby/lib/ruby/1.8/prettyprint.rb:227:in `nest’
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.

Jian L. wrote:

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: