Displaying object variables, not obj ID

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

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.

On 12/23/05, Jim W. [email protected] 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 :wink:

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

Thanks
Tobias

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.

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
Ruby Code & Style - 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