Issue related to class initialization and irb

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

Alle venerdì 29 giugno 2007, darren kirby ha scritto:

secret, but it has no value or use to the user directly ;).

Thanks for consideration,
-d

I think you should redefine the inspect method:

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

I hope this helps

Stefano

On Jun 29, 10:44 am, darren kirby [email protected] wrote:

secret, but it has no value or use to the user directly ;).

Thanks for consideration,
-d

darren kirby :: Part of the problem since 1976 ::http://badcomputer.org
“…the number of UNIX installations has grown to 10, with more expected…”

  • Dennis Ritchie and Ken Thompson, June 1972

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

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?

=> #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.

On Jun 29, 11:11 am, “Dan Stevens (IAmAI)”
[email protected] wrote:

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?

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

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

On Jun 29, 11:17 am, darren kirby [email protected] wrote:

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

quoth the Chris S.:

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:inoutput_value’
from /usr/lib/ruby/1.8/irb.rb:151:in eval_input' from /usr/lib/ruby/1.8/irb.rb:259:insignal_status’
from /usr/lib/ruby/1.8/irb.rb:147:in eval_input' from /usr/lib/ruby/1.8/irb/ruby-lex.rb:244:ineach_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:ineach_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:ineach_top_level_statement’
from /usr/lib/ruby/1.8/irb.rb:146:in eval_input' from /usr/lib/ruby/1.8/irb.rb:70:instart’
from /usr/lib/ruby/1.8/irb.rb:69:in catch' from /usr/lib/ruby/1.8/irb.rb:69:instart’
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