[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Displaying object variables, not obj ID

Tobias Jonch

12/23/2005 12:23:00 PM

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
5 Answers

Jim Weirich

12/23/2005 12:46:00 PM

0

Tobias Jonch 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 Weirich

--
Posted via http://www.ruby-....


Tobias Jonch

12/23/2005 1:25:00 PM

0

On 12/23/05, Jim Weirich <jim@weirichhouse.org> 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

12/23/2005 3:12:00 PM

0

Tobias Jonch wrote:
> On 12/23/05, Jim Weirich <jim@weirichhouse.org> 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?

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.ru... - Ruby Help & Documentation
http://www.artima.c... - Ruby Code & Style: Writers wanted
http://www.rub... - The Ruby Store for Ruby Stuff
http://www.jame... - Playing with Better Toys
http://www.30seco... - Building Better Tools


Tobias Jonch

12/23/2005 3:44:00 PM

0

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

Dave \Crash\ Dummy

6/7/2012 2:48:00 PM

0

On Thu, 7 Jun 2012 06:36:30 -0700 (PDT), KickBoxer GR
<kickboxer861@gmail.com> wrote:

>> Man, you are so out of touch. The entire spectrum of the Greek
>> political world (except, of course, Chrysi Avghi) has condemned this.
>> As for Chrysi Avghi, this is the political end for them.
>
>What makes you dangerous is the fact that you are incapable of
>distinguishing between facts and your wishes.
>
Naturally, as a Chrysi Avghi member, you would say that.