[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Formatting the Object#inspect output

Jeffrey Moss

6/22/2005 9:01:00 PM

When I'm working with large structures in IRB, I get a lot of output that I can't really read all that well, all the #<Array:0xb75713d8 @something=>[#<This:0xb75ca550>, #<That:0xb7521d02>]>

With larger objects I can't really tell what I'm looking for or where to find it, it's just 100+ lines of garbage really, I'd like it to be indented......

Any suggestions how I could format this stuff better?

-Jeff
2 Answers

Ryan Leavengood

6/22/2005 9:23:00 PM

0

Jeffrey Moss said:
> When I'm working with large structures in IRB, I get a lot of output that
> I can't really read all that well, all the #<Array:0xb75713d8
> @something=>[#<This:0xb75ca550>, #<That:0xb7521d02>]>
>
> With larger objects I can't really tell what I'm looking for or where to
> find it, it's just 100+ lines of garbage really, I'd like it to be
> indented......
>
> Any suggestions how I could format this stuff better?

You know you can override inspect, right?

irb(main):001:0> class Foo
irb(main):002:1> def initialize
irb(main):003:2> @a=['a','b','c']
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> f = Foo.new
=> #<Foo:0x2b5fb48 @a=["a", "b", "c"]>
irb(main):007:0> class Foo
irb(main):008:1> def inspect
irb(main):009:2> "a is #{@a.inspect}"
irb(main):010:2> end
irb(main):011:1> end
=> nil
irb(main):012:0> f
=> a is ["a", "b", "c"]

Also, take a look at pp (pretty printer.)

Ryan


Robert Klemme

6/22/2005 9:34:00 PM

0

Jeffrey Moss <jeff@opendbms.com> wrote:
> When I'm working with large structures in IRB, I get a lot of output
> that I can't really read all that well, all the #<Array:0xb75713d8
> @something=>[#<This:0xb75ca550>, #<That:0xb7521d02>]>
>
> With larger objects I can't really tell what I'm looking for or where
> to find it, it's just 100+ lines of garbage really, I'd like it to be
> indented......
>
> Any suggestions how I could format this stuff better?
>
> -Jeff

Try

require 'pp'

pp <cour complex data structure here>

This might look a bit better. Alternatively you can implement #inspect in
your classes to return any string representation you like. This will also
be used by IRB.

Kind regards

robert