Pp and (my) sstruct

OK, I’ve narrowed the problem down a little further.

I have this sstruct class which is like Struct or OpenStruct but with
some other features.

If you’re curious, see the rubyforge snippet:
http://rubyforge.org/snippet/detail.php?type=snippet&id=25

The point here is, it doesn’t quite work with pp for whatever reason.

Here’s an example showing how an ordinary Struct prettyprints, and
then a so-called SuperStruct for comparison.

This problem is likely related to the nontrivial metaprogramming I’m
doing in sstruct.

Ideas, anyone??

Hal

require 'pp’
require ‘sstruct’

Struct.new(“Bar”,:a,:b,:c,:d,:e,:f,:g,:h,:i)

y = Struct::Bar.new([1,2,3,4],“a string”,
{“a”=>“hash”,“of”=>“some kind.”},
File, nil, [[“an”,“array”],[“of”,“arrays”]],
“blah”, “blah”, “blah”)

pp y

···

Bam = SuperStruct.new(:a,:b,:c,:d,:e,:f,:g,:h,:i)

z = Bam.new([1,2,3,4],“a string”,{“a”=>“hash”,“of”=>“some kind.”},
File, nil, [[“an”,“array”],[“of”,“arrays”]],
“blah”, “blah”, “blah”)

pp z

Output:

#<Struct::Bar
a=[1, 2, 3, 4],
b=“a string”,
c={“of”=>“some kind.”, “a”=>“hash”},
d=File,
e=nil,
f=[[“an”, “array”], [“of”, “arrays”]],
g=“blah”,
h=“blah”,
i=“blah”>
<Bam: a=1234 b=a string c=ofsome kind.ahash d=File e=
f=anarrayofarrays g=blah h=blah i=blah>

Hi –

Hal Fulton hal9000@hypermetrics.com writes:

OK, I’ve narrowed the problem down a little further.

I have this sstruct class which is like Struct or OpenStruct but with
some other features.

If you’re curious, see the rubyforge snippet:
http://rubyforge.org/snippet/detail.php?type=snippet&id=25

The point here is, it doesn’t quite work with pp for whatever reason.

Here’s an example showing how an ordinary Struct prettyprints, and
then a so-called SuperStruct for comparison.

This problem is likely related to the nontrivial metaprogramming I’m
doing in sstruct.

Ideas, anyone??

[…]

Output:

#<Struct::Bar
a=[1, 2, 3, 4],
b=“a string”,
c={“of”=>“some kind.”, “a”=>“hash”},
d=File,
e=nil,
f=[[“an”, “array”], [“of”, “arrays”]],
g=“blah”,
h=“blah”,
i=“blah”>
<Bam: a=1234 b=a string c=ofsome kind.ahash d=File e=
f=anarrayofarrays g=blah h=blah i=blah>

You’ve defined #inspect, and that’s what’s being used. From pp.rb:

If (({self})) has a customized (redefined) (({inspect})) method,
the result of (({self.inspect})) is used but it obviously has no
line break hints.

David

···


David A. Black
dblack@wobblini.net

David Alan Black wrote:

You’ve defined #inspect, and that’s what’s being used. From pp.rb:

If (({self})) has a customized (redefined) (({inspect})) method,
the result of (({self.inspect})) is used but it obviously has no
line break hints.

Quite right, David. That does the trick.

Now I owe you two dinners. But they can’t both be at that little
Mexican place.

Hmm, what if I want an inspect (for those who don’t use pp), but I
want it “pretty” for those who do?

There must be an alternative I’m overlooking.

Hal

In article 40B29029.4080108@hypermetrics.com,
Hal Fulton hal9000@hypermetrics.com writes:

Hmm, what if I want an inspect (for those who don’t use pp), but I
want it “pretty” for those who do?

Define pretty_print and alias inspect pretty_print_inspect.

···


Tanaka Akira

Tanaka Akira wrote:

In article 40B29029.4080108@hypermetrics.com,
Hal Fulton hal9000@hypermetrics.com writes:

Hmm, what if I want an inspect (for those who don’t use pp), but I
want it “pretty” for those who do?

Define pretty_print and alias inspect pretty_print_inspect.

Thanks very much for this reply. Always a pleasure to get advice
from the expert.

However, 1) I don’t understand this and 2) it isn’t working for me.

I am doing:

 klass.class_eval do
   # ...
   define_method(:pretty_print) do
     str = "#<#{self.class}:"
     table.each {|item| str << " #{item}=#{self.send(item)}" }
     str + ">"
   end
   alias inspect pretty_print_inspect
   #...
 end

But when I do a pp of a SuperStruct, I get a blank line out.
And if I remove the pp library and call p, it complains that
pretty_print_inspect is not defined. How could this work without
“require ‘pp’”?

Thank you for any clarification.

Hal Fulton

In article 40B2A65D.2000502@hypermetrics.com,
Hal Fulton hal9000@hypermetrics.com writes:

But when I do a pp of a SuperStruct, I get a blank line out.

Copy pretty_print from Struct#pretty_print in pp.rb.

And if I remove the pp library and call p, it complains that
pretty_print_inspect is not defined. How could this work without
“require ‘pp’”?

If you don’t require pp.rb, pp.rb cannot help you.
Define inspect yourself.

···


Tanaka Akira

Tanaka Akira wrote:

And if I remove the pp library and call p, it complains that
pretty_print_inspect is not defined. How could this work without
“require ‘pp’”?

If you don’t require pp.rb, pp.rb cannot help you.
Define inspect yourself.

My goal was to have a normal inspect that did not depend
on pp.rb, but allow prettyprinting if pp.rb was used.

I thought you meant there was some kind of magic going on.
No magic here, I guess.

I suppose I can do

if defined? pp
alias inspect pretty_print_inspect
else
def inspect
# …
end
end

Is this acceptable, or is there a better way?

Thank you,
Hal

In article 40B2A991.8000504@hypermetrics.com,
Hal Fulton hal9000@hypermetrics.com writes:

My goal was to have a normal inspect that did not depend
on pp.rb, but allow prettyprinting if pp.rb was used.

Define both inspect and pretty_print.

I suppose I can do

if defined? pp
alias inspect pretty_print_inspect
else
def inspect
# …
end
end

Is this acceptable, or is there a better way?

You don’t need `if’. Just define inspect as you like.
pp.rb doesn’t use inspect if pretty_print is defined.

···


Tanaka Akira

Tanaka Akira wrote:

Is this acceptable, or is there a better way?

You don’t need `if’. Just define inspect as you like.
pp.rb doesn’t use inspect if pretty_print is defined.

Oh, I see now. This is the puzzle piece that I was missing.

Thanks,
Hal