If I then run: 'puts sandy.address' it correctly outputs the address.
This bit of code was given to me in the tutorial. However if I type
'puts sandy.person' I get the following error message:
'test_temp1.rb:43: undefined method `person' for #<Person:0x2f2324c>
(NoMethodError)'
I am lost because the def to_s inside the Person class looks exactly
like the def to_s in the Address class. What am I missing or doing
wrong? Thanks in advance!
This bit of code was given to me in the tutorial. However if I type
'puts sandy.person' I get the following error message:
'test_temp1.rb:43: undefined method `person' for #<Person:0x2f2324c>
(NoMethodError)'
This is because you are asking it to find the return value of method
'person', but such a method does not exist. With sandy.address, you
asked for the return value of method 'address'. This basically was
returning the object 'address' for sandy. To get the to_s method in
person, just put -
If I then run: 'puts sandy.address' it correctly outputs the address.
This bit of code was given to me in the tutorial. However if I type
'puts sandy.person' I get the following error message:
'test_temp1.rb:43: undefined method `person' for #<Person:0x2f2324c>
(NoMethodError)'
There is no method called `person` in your Person class. If you want to
print the string representation of `sandy`, just do a `puts sandy`.
I get this "#<Person:0x2b3340c>", is this because the Person class makes
reference to the Address class, and its the address class has its own
to_s that prints? Should I move the @street + "\n" + \, ...etc to the
Person class from the Address class?
I get a different error message:
"test_temp1.rb:25:in `to_s': undefined method `+' for
#<Address:0x3413f68> (NoMethodError)
from test_temp1.rb:41:in `puts'
from test_temp1.rb:41"
You are right, the Address class does have its own #to_s. The issue
here is that when you type @address, you are not automatically calling
it. You need to do @address.to_s. (The puts method does
automatically call #to_s on its arguments, but it is not recursive).
Also, you probably want a '+' after the @email + "\n". Methods return
the value of the last line, so without you will only return the
address, not the name and email from the previous lines.
-Adam
···
On 8/28/08, Brian A. <judobrian+ruby-forum@gmail.com> wrote:
I get this "#<Person:0x2b3340c>", is this because the Person class makes
reference to the Address class, and its the address class has its own
to_s that prints? Should I move the @street + "\n" + \, ...etc to the
Person class from the Address class?
On 8/28/08, Brian A. <judobrian+ruby-forum@gmail.com> wrote:
========================================================================
Person class from the Address class?
You are right, the Address class does have its own #to_s. The issue
here is that when you type @address, you are not automatically calling
it. You need to do @address.to_s. (The puts method does
automatically call #to_s on its arguments, but it is not recursive).
Also, you probably want a '+' after the @email + "\n". Methods return
the value of the last line, so without you will only return the
address, not the name and email from the previous lines.