On 5/19/10, Jian Lin <blueskybreeze / gmail.com> wrote: > Dhruva Sagar 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 <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.