pp and (my) sstruct

H

Hal Fulton

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>
 
D

David Alan Black

Hi --

Hal Fulton said:
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
 
H

Hal Fulton

David said:
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
 
T

Tanaka Akira

Hal Fulton said:
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.
 
H

Hal Fulton

Tanaka said:
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
 
T

Tanaka Akira

Hal Fulton said:
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.
 
H

Hal Fulton

Tanaka said:
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
 
T

Tanaka Akira

Hal Fulton said:
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.
 
H

Hal Fulton

Tanaka said:
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
 

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

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top