# Displaying object variables, not obj ID

**URL:** <https://www.ruby-forum.com/t/displaying-object-variables-not-obj-id/50986>\
**Category:** Ruby\
**Created:** [December 23, 2005, 1:24pm UTC](https://www.ruby-forum.com/t/displaying-object-variables-not-obj-id/50986 "2005-12-23T13:24:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tobias\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47774/32.png) [@Tobias\_J](https://www.ruby-forum.com/u/Tobias_J)\
**Post date:** [December 23, 2005, 1:24pm UTC](https://www.ruby-forum.com/t/displaying-object-variables-not-obj-id/50986/1 "2005-12-23T13:24:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tobias\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47774/32.png) [@Tobias\_J](https://www.ruby-forum.com/u/Tobias_J)\
**Post date:** [December 23, 2005, 1:46pm UTC](https://www.ruby-forum.com/t/displaying-object-variables-not-obj-id/50986/2 "2005-12-23T13:46:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tobias\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47774/32.png) [@Tobias\_J](https://www.ruby-forum.com/u/Tobias_J)\
**Post date:** [December 23, 2005, 2:28pm UTC](https://www.ruby-forum.com/t/displaying-object-variables-not-obj-id/50986/3 "2005-12-23T14:28:12Z")

</div>

On 12/23/05, Jim W. [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![Tobias\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47774/32.png) [@Tobias\_J](https://www.ruby-forum.com/u/Tobias_J)\
**Post date:** [December 23, 2005, 4:46pm UTC](https://www.ruby-forum.com/t/displaying-object-variables-not-obj-id/50986/4 "2005-12-23T16:46:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tobias\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47774/32.png) [@Tobias\_J](https://www.ruby-forum.com/u/Tobias_J)\
**Post date:** [December 23, 2005, 4:13pm UTC](https://www.ruby-forum.com/t/displaying-object-variables-not-obj-id/50986/5 "2005-12-23T16:13:45Z")

</div>

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](http://www.ruby-doc.org) - Ruby Help & Documentation  
[Ruby Code & Style](http://www.artima.com/rubycs/) - Ruby Code & Style: Writers wanted  
[http://www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
[http://www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys  
[http://www.30secondrule.com](http://www.30secondrule.com) - Building Better Tools
