# Issue related to class initialization and irb

**URL:** <https://www.ruby-forum.com/t/issue-related-to-class-initialization-and-irb/108174>\
**Category:** Ruby\
**Created:** [June 29, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/issue-related-to-class-initialization-and-irb/108174 "2007-06-29T18:45:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [June 29, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/issue-related-to-class-initialization-and-irb/108174/1 "2007-06-29T18:45:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [June 29, 2007, 6:57pm UTC](https://www.ruby-forum.com/t/issue-related-to-class-initialization-and-irb/108174/2 "2007-06-29T18:57:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [June 29, 2007, 7:01pm UTC](https://www.ruby-forum.com/t/issue-related-to-class-initialization-and-irb/108174/3 "2007-06-29T19:01:54Z")

</div>

On Jun 29, 10:44 am, darren kirby [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](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

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [June 29, 2007, 7:12pm UTC](https://www.ruby-forum.com/t/issue-related-to-class-initialization-and-irb/108174/4 "2007-06-29T19:12:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [June 29, 2007, 7:07pm UTC](https://www.ruby-forum.com/t/issue-related-to-class-initialization-and-irb/108174/5 "2007-06-29T19:07:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [June 29, 2007, 7:20pm UTC](https://www.ruby-forum.com/t/issue-related-to-class-initialization-and-irb/108174/6 "2007-06-29T19:20:55Z")

</div>

On Jun 29, 11:11 am, “Dan Stevens (IAmAI)”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [June 29, 2007, 7:18pm UTC](https://www.ruby-forum.com/t/issue-related-to-class-initialization-and-irb/108174/7 "2007-06-29T19:18:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [June 29, 2007, 7:25pm UTC](https://www.ruby-forum.com/t/issue-related-to-class-initialization-and-irb/108174/8 "2007-06-29T19:25:56Z")

</div>

On Jun 29, 11:17 am, darren kirby [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [June 29, 2007, 11:21pm UTC](https://www.ruby-forum.com/t/issue-related-to-class-initialization-and-irb/108174/9 "2007-06-29T23:21:27Z")

</div>

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