Displaying object variables, not obj ID

T

Tobias Jonch

------=_Part_11884_446215.1135340581206
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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' =3D>
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

------=_Part_11884_446215.1135340581206--
 
J

Jim Weirich

Tobias said:
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.
 
T

Tobias Jonch

------=_Part_12136_29566405.1135344309789
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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

------=_Part_12136_29566405.1135344309789--
 
J

James Britt

Tobias said:
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.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
 
T

Tobias Jonch

------=_Part_12620_29870508.1135352639413
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
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.

------=_Part_12620_29870508.1135352639413--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top