Issue related to class initialization and irb

D

darren kirby

Hello all,

My class is a library that allows users to read information/tags etc from Flac
files.

When the class is initialized in irb the content of every single instance
variable is dumped to the console. This library parses a lot of data from
various blocks, and has many data structures to hold it all. As such, this
output can contain as much as two screens worth. Some of this data is
non-readable and is for 'top-secret internal use only'. That is, it isn't
secret, but it has no value or use to the user directly ;).

Anyway, I would really like to clean up this output and have irb print only
eg:

=> #<FlacInfo:0x6f97cca4>

Or perhaps the above with a brief enumeration of the blocks found and their
sizes and/or offsets. To this end I tried defining the class' to_s method,
and modifying the initialize method's return value. Neither had an effect.
How might I accomplish this?

Thanks for consideration,
-d
 
C

Chris Shea

Hello all,

My class is a library that allows users to read information/tags etc from Flac
files.

When the class is initialized in irb the content of every single instance
variable is dumped to the console. This library parses a lot of data from
various blocks, and has many data structures to hold it all. As such, this
output can contain as much as two screens worth. Some of this data is
non-readable and is for 'top-secret internal use only'. That is, it isn't
secret, but it has no value or use to the user directly ;).

Anyway, I would really like to clean up this output and have irb print only
eg:

=> #<FlacInfo:0x6f97cca4>

Or perhaps the above with a brief enumeration of the blocks found and their
sizes and/or offsets. To this end I tried defining the class' to_s method,
and modifying the initialize method's return value. Neither had an effect.
How might I accomplish this?

Thanks for consideration,
-d

What you want to do is redefine the inspect method.

irb(main):001:0> class Test
irb(main):002:1> attr_accessor :foo, :bar
irb(main):003:1> end
=> nil
irb(main):004:0> a = Test.new
=> #<Test:0x100daec>
irb(main):005:0> a.foo = 42
=> 42
irb(main):006:0> a.bar = "a place to buy drinks"
=> "a place to buy drinks"
irb(main):007:0> a
=> #<Test:0x100daec @bar="a place to buy drinks", @foo=42>
irb(main):008:0> class Test
irb(main):009:1> def inspect
irb(main):010:2> "#<Test:#{self.object_id}>"
irb(main):011:2> end
irb(main):012:1> end
=> nil
irb(main):013:0> a
=> #<Test:8416630>

HTH,
Chris
 
S

Stefano Crocco

Alle venerd=EC 29 giugno 2007, darren kirby ha scritto:
Hello all,

My class is a library that allows users to read information/tags etc from
Flac files.

When the class is initialized in irb the content of every single instance
variable is dumped to the console. This library parses a lot of data from
various blocks, and has many data structures to hold it all. As such, this
output can contain as much as two screens worth. Some of this data is
non-readable and is for 'top-secret internal use only'. That is, it isn't
secret, but it has no value or use to the user directly ;).

Anyway, I would really like to clean up this output and have irb print on= ly
eg:

=3D> #<FlacInfo:0x6f97cca4>

Or perhaps the above with a brief enumeration of the blocks found and the= ir
sizes and/or offsets. To this end I tried defining the class' to_s method,
and modifying the initialize method's return value. Neither had an effect.
How might I accomplish this?

Thanks for consideration,
-d

I think you should redefine the inspect method:

irb: 006> class C
irb: 007+> def initialize x
irb: 008+> @x =3D x
irb: 009+> end
irb: 010+> def inspect
irb: 011+> "C: x=3D#{@x}"
irb: 012+> end
irb: 013+> end
irb: 015> C.new 2
=3D> C: x2

I hope this helps

Stefano
 
D

Dan Stevens (IAmAI)

When the class is initialized in irb the content of every single instance
variable is dumped to the console. This library parses a lot of data from
various blocks, and has many data structures to hold it all. As such, this
output can contain as much as two screens worth. Some of this data is
non-readable and is for 'top-secret internal use only'. That is, it isn't
secret, but it has no value or use to the user directly ;).

Anyway, I would really like to clean up this output and have irb print only
eg:

=> #<FlacInfo:0x6f97cca4>

Try defining a method called 'inspect' instead of 'to_s'. I'll assume
you know how to form the desired string, as I don't know what methods
are required to do that off the top of my head.
 
D

Dan Stevens (IAmAI)

irb(main):004:0> a = Test.new
=> #<Test:0x100daec>
irb(main):013:0> a
=> #<Test:8416630>

Does anyone know why the value returned by Object#object_id is
different to the value returned by the 'new' method? Is one hex and
the other is not?
 
P

Phrogz

Does anyone know why the value returned by Object#object_id is
different to the value returned by the 'new' method? Is one hex and
the other is not?

Please search the mailing list - I believe this has been discussed
twice in the last few weeks.
 
D

darren kirby

First off, thanks to Stefano, Chris, and Dan. inspect was what I was looking
for and I have it printing what I would like.

quoth the Dan Stevens (IAmAI):
Does anyone know why the value returned by Object#object_id is
different to the value returned by the 'new' method? Is one hex and
the other is not?

This is a follow-up question for me as well. Even when the object_id value is
represented in hex it does not match the default output. I guess the default
value is the object's address in memory or somesuch?

Looking through 'Object', 'Kernel', and a few others in the Pickaxe book, I
can find no relevant method (which doesn't mean it isn't there ;)). Is there
a way to get this value? Does it really matter or is it just cosmetic?

thanks,
-d
 
C

Chris Shea

This is a follow-up question for me as well. Even when the object_id value is
represented in hex it does not match the default output. I guess the default
value is the object's address in memory or somesuch?

The hex value is double the object_id. So in my example, redefining
inspect as

"#<Test:0x#{(self.object_id*2).to_s(16)}>"

would give you what you're expecting.

Chris
 
D

darren kirby

quoth the Chris Shea:
The hex value is double the object_id. So in my example, redefining
inspect as

"#<Test:0x#{(self.object_id*2).to_s(16)}>"

would give you what you're expecting.

Chris

Thanks again Chris.

I still have one more issue here. My knowledge of access control is admittedly
superficial, but since 'inspect' is called internally I should be able to
define it as a private method shouldn't I? Or is it bombing because it is
called from irb?

When I define it privately I get:
-----------------------
irb(main):016:0> song = FlacInfo.new("test.flac")
NoMethodError: private method `inspect' called for #<FlacInfo:0x6f919ec4>
from /usr/lib/ruby/1.8/irb.rb:298:in `output_value'
from /usr/lib/ruby/1.8/irb.rb:151:in `eval_input'
from /usr/lib/ruby/1.8/irb.rb:259:in `signal_status'
from /usr/lib/ruby/1.8/irb.rb:147:in `eval_input'
from /usr/lib/ruby/1.8/irb/ruby-lex.rb:244:in
`each_top_level_statement'
from /usr/lib/ruby/1.8/irb/ruby-lex.rb:230:in `loop'
from /usr/lib/ruby/1.8/irb/ruby-lex.rb:230:in
`each_top_level_statement'
from /usr/lib/ruby/1.8/irb/ruby-lex.rb:229:in `catch'
from /usr/lib/ruby/1.8/irb/ruby-lex.rb:229:in
`each_top_level_statement'
from /usr/lib/ruby/1.8/irb.rb:146:in `eval_input'
from /usr/lib/ruby/1.8/irb.rb:70:in `start'
from /usr/lib/ruby/1.8/irb.rb:69:in `catch'
from /usr/lib/ruby/1.8/irb.rb:69:in `start'
from /usr/bin/irb:13
Maybe IRB bug!!
-------------------------
Is this really a bug or is it just a necessary product of calling from irb?

I don't really need or want this as a public method. I suppose I could just
make it so Rdoc doesn't document it, as it isn't like it is going to hurt
anything if called directly.

-d
 

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,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top