Forum: Ruby Issue related to class initialization and irb

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
darren kirby (Guest)
on 2007-06-29 20:45
(Received via mailing list)
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
Stefano C. (Guest)
on 2007-06-29 20:57
(Received via mailing list)
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
Chris S. (Guest)
on 2007-06-29 21:01
(Received via mailing list)
On Jun 29, 10:44 am, darren kirby <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
> "...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
Dan Stevens (IAmAI) (Guest)
on 2007-06-29 21:07
(Received via mailing list)
> =>  #<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.
Dan Stevens (IAmAI) (Guest)
on 2007-06-29 21:12
(Received via mailing list)
> 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?
darren kirby (Guest)
on 2007-06-29 21:18
(Received via mailing list)
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
Gavin K. (Guest)
on 2007-06-29 21:20
(Received via mailing list)
On Jun 29, 11:11 am, "Dan Stevens (IAmAI)"
<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.
Chris S. (Guest)
on 2007-06-29 21:25
(Received via mailing list)
On Jun 29, 11:17 am, darren kirby <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
darren kirby (Guest)
on 2007-06-30 01:21
(Received via mailing list)
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
This topic is locked and can not be replied to.