Forum: Ruby Displaying object variables, not obj ID

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.
Tobias J. (Guest)
on 2005-12-23 14:24
(Received via mailing list)
I'm having problems with displaying an object correctly. I keep getting
the
obj's ID back, like this:

sendto("Chromosomes:   " + p.c_chromosomes.to_s)     #  sort of 'puts'
=>
Chromosomes:   #<Chromosome:0x10267658>

Checking with the Pickaxe, I see they have a solution (no obvious
explanation though), where they suggest redifining to_s to accomodate
its
purpose in their 'song' class. This is no good for me though, as to_s
will
be used to display a lot of things, not just chromosomes.

Uhm. I feel like I should provide more info to ensure an accurate
answer,
but I'm not sure what or how much is necessary.
Perhaps that the variable actually saves right in the yaml file:

...
c_chromosomes: !ruby/object:Chromosome
    c_chromosomes:  # hash of arrays
      symmetry: []
...

Any insights would be much appreciated.
Tobias
Jim W. (Guest)
on 2005-12-23 14:46
Tobias J. wrote:
> Checking with the Pickaxe, I see they have a solution (no obvious
> explanation though), where they suggest redifining to_s to accomodate
> its purpose in their 'song' class. This is no good for me though, as to_s
> will be used to display a lot of things, not just chromosomes.

Perhaps you are not aware that defining a to_s method in the Chromosome
class will only affect how Chromosomes are displayed, nothing else.

--
-- Jim W.
Tobias J. (Guest)
on 2005-12-23 15:28
(Received via mailing list)
On 12/23/05, Jim W. <removed_email_address@domain.invalid> wrote:
>
>
> Perhaps you are not aware that defining a to_s method in the Chromosome
> class will only affect how Chromosomes are displayed, nothing else.


Astonishing as it may seem, I do know ;-)

I'm not calling to_s from the chromosome class, however, and that does
matter, right?

Thanks
Tobias
James B. (Guest)
on 2005-12-23 17:13
(Received via mailing list)
Tobias J. wrote:
> I'm not calling to_s from the chromosome class, however, and that does
> matter, right?

You are invoking the Chromosome version of to_s method on instances of
Chromosome.

When you call to_s on an instance of a class, and you get back that
ClassName:0xID thing, it means that that object does not have its own
to_s method defined, so it defaults to calling Object's to_s (which of
course as no idea how to stingify this particular class).

So you need to give Chromosome instances their own to_s.

James


--

http://www.ruby-doc.org       - Ruby Help & Documentation
http://www.artima.com/rubycs/ - Ruby Code & Style: Writers wanted
http://www.rubystuff.com      - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com     - Playing with Better Toys
http://www.30secondrule.com   - Building Better Tools
Tobias J. (Guest)
on 2005-12-23 17:46
(Received via mailing list)
> So you need to give Chromosome instances their own to_s.
>
> James
>
> Aha. I guess it didn't matter after all.
Thanks James, for your lucid explanation.
This topic is locked and can not be replied to.